Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problem with char type in structures (MSVC)

Reply
Thread Tools

Problem with char type in structures (MSVC)

 
 
h79
Guest
Posts: n/a
 
      08-28-2004
Hi everyone.

I've got a little problem...
I'll try illustrate it on example.

////////////
typedef unsigned long DWORD;
typedef unsigned char BYTE;

struct TST
{
BYTE a, b, c;
DWORD d;
};

int main()
{
ifstream *h_in;
TST s_tst;

h_in = new ifstream("tst.bin", ios::in | ios::binary |
ios::nocreate);
h_in->read((char*)&s_tst, sizeof(TST));
// 004010F0 6A 08 push 8 ; <- why 8, not 7 ?
// 004010F2 8D 45 E8 lea eax,[ebp-18h]
// 004010F5 50 push eax
// 004010F6 8B 4D F0 mov ecx,dword ptr [ebp-10h]
// 004010F9 E8 32 0A 00 00 call istream::read
(00401b30)

cout << hex << (int)s_tst.a << endl;
// printed: '1' <- OK
cout << hex << (int)s_tst.b << endl;
// printed: '2' <- OK
cout << hex << (int)s_tst.c << endl;
// printed: '3' <- OK
cout << hex << s_tst.d << endl;
// printed: 'cc000000' <- ERROR: should be '4'

delete h_in;
return 0;

}

//////////////

Below is the body of file 'tst.bin':

00000000|01 02 03 04-00 00 00

It's length is equal to 7.

Questions are:
1) Why offset of 'd' member i structure 'TST' is equal to 4, not 3?
2) What add in source code or change in project settings to fix it
(i.e. offset of 'd' member be equal 3)?

I want add that this problem apear in MSVC++, but doesn't apear in
Borland C++ compiler where builded program work fine.

Thanks
Harnas
 
Reply With Quote
 
 
 
 
David Hilsee
Guest
Posts: n/a
 
      08-28-2004
"h79" <> wrote in message
news: om...
> Hi everyone.
>
> I've got a little problem...
> I'll try illustrate it on example.
>
> ////////////
> typedef unsigned long DWORD;
> typedef unsigned char BYTE;
>
> struct TST
> {
> BYTE a, b, c;
> DWORD d;
> };
>
> int main()
> {
> ifstream *h_in;
> TST s_tst;
>
> h_in = new ifstream("tst.bin", ios::in | ios::binary |
> ios::nocreate);
> h_in->read((char*)&s_tst, sizeof(TST));
> // 004010F0 6A 08 push 8 ; <- why 8, not 7 ?
> // 004010F2 8D 45 E8 lea eax,[ebp-18h]
> // 004010F5 50 push eax
> // 004010F6 8B 4D F0 mov ecx,dword ptr [ebp-10h]
> // 004010F9 E8 32 0A 00 00 call istream::read
> (00401b30)
>
> cout << hex << (int)s_tst.a << endl;
> // printed: '1' <- OK
> cout << hex << (int)s_tst.b << endl;
> // printed: '2' <- OK
> cout << hex << (int)s_tst.c << endl;
> // printed: '3' <- OK
> cout << hex << s_tst.d << endl;
> // printed: 'cc000000' <- ERROR: should be '4'
>
> delete h_in;
> return 0;
>
> }
>
> //////////////
>
> Below is the body of file 'tst.bin':
>
> 00000000|01 02 03 04-00 00 00
>
> It's length is equal to 7.
>
> Questions are:
> 1) Why offset of 'd' member i structure 'TST' is equal to 4, not 3?
> 2) What add in source code or change in project settings to fix it
> (i.e. offset of 'd' member be equal 3)?
>
> I want add that this problem apear in MSVC++, but doesn't apear in
> Borland C++ compiler where builded program work fine.


The compiler may insert padding in structs to align the members properly.
If you want to change the compiler's behavior, consult its documentation or
a more appropriate newsgroup that discusses your compiler. I wouldn't
bother to change the compiler's settings, because changing the code would be
an easy solution that wouldn't require revisiting this problem later on
other compilers.

h_in->read((char*)&s_tst, sizeof(TST));

// Read each member instead
h_in->read((char*)&s_tst.a, sizeof(BYTE));
h_in->read((char*)&s_tst.b, sizeof(BYTE));
h_in->read((char*)&s_tst.c, sizeof(BYTE));
h_in->read((char*)&s_tst.d, sizeof(DWORD));

--
David Hilsee


 
Reply With Quote
 
 
 
 
h79
Guest
Posts: n/a
 
      08-30-2004
Thanks.

Today I found also another solution about this problem (I think so).
It depends on use '#pragma pack' option. I used '#pragma pack(1)'.
After using this option all members in structures should be stored on
1-byte boundaries. For me it works fine.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
Problem- strcat with char and char indexed from char array aldonnelley@gmail.com C++ 3 04-20-2006 07:32 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
structures, structures and more structures (questions about nestedstructures) Alfonso Morra C Programming 11 09-24-2005 07:42 PM
Type Casting IPv4 and IPv6 structures to Generic Structures tweak C Programming 14 06-11-2004 02:43 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57