[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[open-cobol-list] Re: static cobol 2 c
From: |
Roger While |
Subject: |
[open-cobol-list] Re: static cobol 2 c |
Date: |
Tue Oct 25 00:26:57 2005 |
> what about float/double numbers ?
COMP-1 (AKA FLOAT-SHORT) is always "native" - 4 bytes (float)
COMP-2 (AKA FLOAT-LONG) is always "native" - 8 bytes (double)
Also, a word of warning about alignment :
If you want to pass a group item as a "C structure" (and reference it as such -
ie. struct mystruct * ),
make sure that the group item is a 01 level.
Why ? - On i386 and "alignment tolerant" architectures you can get away with
casting anything to anything. On other boxes this is definitely not so.
Further, if we take your example :
> typedef struct abcd {
> char phrase[10];
> int compteur ;
> } efgh;
and assuming (as previously said) that we have used the "pack" attribute,
then the 4 byte "compteur" field is not naturally aligned.
Once again, on alignment tolerant machines, you get away with this.
On non-alignment tolerant architectures, you will get a bus error when
you access compteur.
(unless GCC is clever enough to sort this out - Anybody know ?)
Note that all of this thread also applies to MF and ACU Cobol.
The absolute correct and poratable way to communicate with C is
to assume everything is characters eg.
int my_c_routine ( char * mygrouppointer )
{
struct {
char phrase[10];
char compteur[4];
} mystruct;
int mylocalcompteur;
/* Note we must use 14 and not "sizeof" here */
(void)memcpy ((char *)&mystruct, mygrouppointer, 14);
(void)memcpy ((char *)&mylocalcompteur, mystruct.compteur, sizeof(int));
........
}
And, if that isn't enough, be aware of fields that have "USAGE POINTER" -
These are 4 bytes on 32-bit boxes and 8 bytes on 64-bit boxes.
So, you would need something like this for POINTER items :
01 MYGROUP.
03 MYALPHA PIC X(10).
03 MYPOINTER USAGE POINTER.
#define MY_POINTER_LENGTH sizeof(char *)
int my_c_routine ( char * mygrouppointer )
{
struct {
char phrase[10];
char mypointer[MY_POINTER_LENGTH];
} mystruct;
char * mylocalpointer;
/* Note we must use 10 + .. and not "sizeof" here */
(void)memcpy ((char *)&mystruct, mygrouppointer, 10 +
MY_POINTER_LENGTH);
(void)memcpy ((char *)&mylocalpointer, mystruct.mypointer,
MY_POINTER_LENGTH);
........
}
Roger