BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

struct member addressing problems in BC5.5.1 ?

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Command Line Tools)
View previous topic :: View next topic  
Author Message
nss
Guest





PostPosted: Sun Mar 06, 2005 5:10 am    Post subject: struct member addressing problems in BC5.5.1 ? Reply with quote



Hi

I have a program which works correctly under BC4.52 and BC5.02 but
it fails under BC5.5.1 (FreeCommandLineTools).

The program fails when I point a struct to some binary data
and then try to manipulate the data through the structure members.
Like the following:

char *BinBuff; /* binary data buffer */

struct mystruct *mst;

mst = (struct mystruct *)BinBuff;

mst->mem01 = 0x10;
mst->mem03 = 0x55;
....etc

I have narrowed it down to few lines of code to demonstrate the
behaviour. I compiled this code using the commandline:
"bcc32 -c -S sizetest.c" under both BC5.02 and BC5.5.1. to generate
assembly listings.

I am posting the source code and the two different .asm listings
which clearly show that the BC5.5.1 compiler is messing up the
structure member addressing.

I have marked the differences with //<<<<< which need to be looked at.

Please advise whether it is a fault on my part or something else.



/*******************************************************************/
/* source code */
/*******************************************************************/
struct mystruct {
short member1;
char member2;
short member3;
};

int main()
{
struct mystruct ss;
struct mystruct *s;

s = &ss;
s->member1 = 32767;
s->member2 = 'A';
s->member3 = 32767;

printf("s->member1 = %dn",s->member1 );
printf("s->member2 = %dn",s->member2 );
printf("s->member3 = %dn",s->member3 );

return 0;
}



/*******************************************************************/
/* asm listing for BC5.02 */
/*******************************************************************/
.386p
model flat
ifndef ??version
?debug macro
endm
endif
?debug S "sizetest.c"
?debug T "sizetest.c"
_TEXT segment dword public use32 'CODE'
_TEXT ends
_DATA segment dword public use32 'DATA'
_DATA ends
_BSS segment dword public use32 'BSS'
_BSS ends
DGROUP group _BSS,_DATA
_DATA segment dword public use32 'DATA'
align 4
__stklen label dword
dd 8192
_DATA ends
_TEXT segment dword public use32 'CODE'
_main proc near
?live1@0:
;
; int main()
;
push ebp
mov ebp,esp
add esp,-8
push ebx
;
; {
; struct mystruct ss;
; struct mystruct *s;
;
; s = &ss;
;
@1:
lea ebx,dword ptr [ebp-8]
;
; s->member1 = 32767;
;
?live1@32: ; EBX = s
mov word ptr [ebx],32767
;
; s->member2 = 'A';
;
mov byte ptr [ebx+2],65
;
; s->member3 = 32767;
;
mov word ptr [ebx+3],32767 //<<<<<
;
;
; printf("s->member1 = %dn",s->member1 );
;
movsx eax,word ptr [ebx]
push eax
push offset s@
call _printf
add esp,8
;
; printf("s->member2 = %dn",s->member2 );
;
movsx edx,byte ptr [ebx+2]
push edx
push offset s@+16
call _printf
add esp,8
;
; printf("s->member3 = %dn",s->member3 );
;
movsx ecx,word ptr [ebx+3] //<<<<<
push ecx
push offset s@+32
call _printf
add esp,8
;
;
; return 0;
;
?live1@128: ;
xor eax,eax
;
; }
;
@3:
@2:
pop ebx
pop ecx
pop ecx
pop ebp
ret
_main endp
_TEXT ends
_DATA segment dword public use32 'DATA'
s@ label byte
; s@+0:
db "s->member1 = %d",10,0
; s@+16:
db "s->member2 = %d",10,0
; s@+32:
db "s->member3 = %d",10,0
align 4
_DATA ends
_TEXT segment dword public use32 'CODE'
_TEXT ends
public __stklen
public _main
extrn _printf:near
?debug D "C:PBORLANDBC5INCLUDEio.h" 8825 10304
?debug D "C:PBORLANDBC5INCLUDEprocess.h" 8825 10304
?debug D "C:PBORLANDBC5INCLUDEtchar.h" 8825 10304
?debug D "C:PBORLANDBC5INCLUDEdir.h" 8825 10304
?debug D "C:PBORLANDBC5INCLUDEalloc.h" 8825 10304
?debug D "C:PBORLANDBC5INCLUDEctype.h" 8825 10304
?debug D "C:PBORLANDBC5INCLUDEstddef.h" 8825 10304
?debug D "C:PBORLANDBC5INCLUDEstring.h" 8825 10304
?debug D "C:PBORLANDBC5INCLUDEstdarg.h" 8825 10304
?debug D "C:PBORLANDBC5INCLUDEstdlib.h" 8825 10304
?debug D "C:PBORLANDBC5INCLUDEdos.h" 8825 10304
?debug D "C:PBORLANDBC5INCLUDE_null.h" 8825 10304
?debug D "C:PBORLANDBC5INCLUDE_nfile.h" 8825 10304
?debug D "C:PBORLANDBC5INCLUDE_defs.h" 8825 10304
?debug D "C:PBORLANDBC5INCLUDEstdio.h" 8825 10304
?debug D "sizetest.c" 12902 19215
end


/*******************************************************************/
/* asm listing for BC5.5.1 */
/*******************************************************************/
.386p
ifdef ??version
if ??version GT 500H
.mmx
endif
endif
model flat
ifndef ??version
?debug macro
endm
endif
?debug S "sizetest.c"
?debug T "sizetest.c"
_TEXT segment dword public use32 'CODE'
_TEXT ends
_DATA segment dword public use32 'DATA'
_DATA ends
_BSS segment dword public use32 'BSS'
_BSS ends
DGROUP group _BSS,_DATA
_DATA segment dword public use32 'DATA'
align 4
__stklen label dword
dd 8192
_DATA ends
_TEXT segment dword public use32 'CODE'
_main proc near
?live1@0:
;
; int main()
;
push ebp
mov ebp,esp
add esp,-8
push ebx
;
; {
; struct mystruct ss;
; struct mystruct *s;
;
; s = &ss;
;
@1:
lea ebx,dword ptr [ebp-8]
;
; s->member1 = 32767;
;
?live1@32: ; EBX = s
mov word ptr [ebx],32767
;
; s->member2 = 'A';
;
mov byte ptr [ebx+2],65
;
; s->member3 = 32767;
;
mov word ptr [ebx+4],32767 //<<<<<
;
;
; printf("s->member1 = %dn",s->member1 );
;
movsx eax,word ptr [ebx]
push eax
push offset s@
call _printf
add esp,8
;
; printf("s->member2 = %dn",s->member2 );
;
movsx edx,byte ptr [ebx+2]
push edx
push offset s@+16
call _printf
add esp,8
;
; printf("s->member3 = %dn",s->member3 );
;
movsx ecx,word ptr [ebx+4] //<<<<<
push ecx
push offset s@+32
call _printf
add esp,8
;
;
; return 0;
;
?live1@128: ;
xor eax,eax
;
; }
;
@3:
@2:
pop ebx
pop ecx
pop ecx
pop ebp
ret
_main endp
_TEXT ends
_DATA segment dword public use32 'DATA'
s@ label byte
; s@+0:
db "s->member1 = %d",10,0
; s@+16:
db "s->member2 = %d",10,0
; s@+32:
db "s->member3 = %d",10,0
align 4
_DATA ends
_TEXT segment dword public use32 'CODE'
_TEXT ends
public __stklen
public _main
extrn _printf:near
?debug D "c:PBorlandBc55includeio.h" 10459 10272
?debug D "c:PBorlandBc55includeprocess.h" 10459 10272
?debug D "c:PBorlandBc55includetchar.h" 10459 10272
?debug D "c:PBorlandBc55includedir.h" 10459 10272
?debug D "c:PBorlandBc55includealloc.h" 10459 10272
?debug D "c:PBorlandBc55includembctype.h" 10459 10272
?debug D "c:PBorlandBc55includectype.h" 10459 10272
?debug D "c:PBorlandBc55includemem.h" 10459 10272
?debug D "c:PBorlandBc55include_loc.h" 10459 10272
?debug D "c:PBorlandBc55includelocale.h" 10459 10272
?debug D "c:PBorlandBc55include_str.h" 10459 10272
?debug D "c:PBorlandBc55includestring.h" 10459 10272
?debug D "c:PBorlandBc55includestdarg.h" 10459 10272
?debug D "c:PBorlandBc55includesearch.h" 10459 10272
?debug D "c:PBorlandBc55includestdlib.h" 10459 10272
?debug D "c:PBorlandBc55includeerrno.h" 10459 10272
?debug D "c:PBorlandBc55includedos.h" 10459 10272
?debug D "c:PBorlandBc55include_nfile.h" 10459 10272
?debug D "c:PBorlandBc55include_null.h" 10459 10272
?debug D "c:PBorlandBc55include_defs.h" 10459 10272
?debug D "c:PBorlandBc55include_stddef.h" 10459 10272
?debug D "c:PBorlandBc55includestdio.h" 10459 10272
?debug D "sizetest.c" 12902 19215
end
/*******************************************************************/

Back to top
nss
Guest





PostPosted: Mon Mar 07, 2005 11:21 pm    Post subject: Re: struct member addressing problems in BC5.5.1 ? Reply with quote



I have figured it out to be a structure alignment (padding) problem
and it can be fixed by using a one byte alignment. All compilers have
vendor implementation specific defaults for alignment.

How do I instruct the compiler to use a one byte alignment for my
struct declarations for all the different compiler versions (or
brands)?

Is there any portable method of instructing a compiler to use a
particular struct alignment?

Thanks
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Command Line Tools) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.