473,322 Members | 1,425 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Problem with char type in structures (MSVC)

h79
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
Jul 22 '05 #1
2 1777
"h79" <h7*@interia.pl> wrote in message
news:90**************************@posting.google.c 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
Jul 22 '05 #2
h79
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.
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: TK | last post by:
I have encountered a very strange bug. I am using a variable defined as: char ch; If I assigned a character like 'Ö' (ascii 153) with a statement like: ch = 'Ö'; It gets the value -103....
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
4
by: NT | last post by:
Hi there! I am puzzled by what I think must be a subtlety of C++ when list-initializing member variables in derived classes... Here's what I mean... class A { // warning l337proggy alert!...
39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
23
by: Babak | last post by:
Hi Everyone, I've written a standard C code for a simple finite element analysis in MSVC++ . When I save the file as a cpp file, it compiles and runs perfectly, but when I save it as a c file,...
9
by: Roman Mashak | last post by:
Hello, In the program I'm working on I receive a data stream, having structure as follows: | common_header | I2C_header | data ... |
11
by: Gerald I. Evenden | last post by:
Working on a Kubuntu 64bit system "c++ (GCC) 4.0.3". The following simple program extracted from p.497 & 499 of N.M.Josurris' "The C++ Standard Library ... " (file t.cpp): 1 #include <string>...
3
by: imaloner | last post by:
I am posting two threads because I have two different problems, but both have the same background information. Common Background Information: I am trying to rebuild code for a working,...
7
by: edsunder | last post by:
I'm making a "wrapper" for an unmanaged DLL (written, I'm fairly certain in C). I have a c++ "wrapper" that I'm trying to convert to VB.net. I've got most of the program working, but I've hit a brick...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.