Exetools  

Go Back   Exetools > General > General Discussion

Notices

Reply
 
Thread Tools Display Modes
  #1  
Old 08-19-2005, 06:36
Cobi Cobi is offline
Friend
 
Join Date: Sep 2004
Location: Germany
Posts: 55
Rept. Given: 0
Rept. Rcvd 0 Times in 0 Posts
Thanks Given: 0
Thanks Rcvd at 1 Time in 1 Post
Cobi Reputation: 0
sizeof() returns "wrong" Size of structure

Why is this structure 20 Bytes in size?
Code:
typedef	struct STGAHeader{

	BYTE	m_uIDSize;
	BYTE	m_uColorMapType;
	BYTE	m_uImageType;

	short	m_sColorMapStart;
	short	m_sColorMapLength;
	BYTE	m_uColorMapBits;

	short	m_sXStart;
	short	m_sYStart;
	short	m_sWidth;
	short	m_sHeight;
	BYTE	m_uBits;
	BYTE	m_uDescriptor;

} STGAHeader;
(BYTE = unsigned char = 1 byte, short = 2 bytes)
For me 1+1+1+2+2+1+2+2+2+2+1+1 is 18 and not 20 but the compiler thinks different
Its absolutely impossible that 6*integer1+6*integer2 is equal 20
(Intel 8.0 win)
thx, Cobi

Last edited by Cobi; 08-19-2005 at 06:39.
Reply With Quote
  #2  
Old 08-19-2005, 06:49
tom324 tom324 is offline
Friend
 
Join Date: Jan 2002
Posts: 231
Rept. Given: 5
Rept. Rcvd 7 Times in 6 Posts
Thanks Given: 21
Thanks Rcvd at 27 Times in 16 Posts
tom324 Reputation: 7
Try with

#paragma pack(1) (or Intel 8.0 equivalent) directive to have alignment set to 1.

Tom
Reply With Quote
  #3  
Old 08-19-2005, 07:04
Cobi Cobi is offline
Friend
 
Join Date: Sep 2004
Location: Germany
Posts: 55
Rept. Given: 0
Rept. Rcvd 0 Times in 0 Posts
Thanks Given: 0
Thanks Rcvd at 1 Time in 1 Post
Cobi Reputation: 0
Seems to work, thx.
Guess i need to learn my tools
Reply With Quote
  #4  
Old 08-19-2005, 16:51
Mkz Mkz is offline
Friend
 
Join Date: Jan 2002
Posts: 98
Rept. Given: 0
Rept. Rcvd 2 Times in 2 Posts
Thanks Given: 5
Thanks Rcvd at 25 Times in 17 Posts
Mkz Reputation: 2
Just to make sure you know the *reason*:

After "m_uImageType" and before "m_sColorMapStart", the compiler inserted a padding "0", so that the 2-byte-wide "m_sColorMapStart" will be stored at an address which is a multiple of 2. If you had a DWORD, the compiler would pad with zeroes until the address would be a multiple of 4, and so on.

The pragma directive will tell the compiler not to bother aligning the structures.

If you follow the address of each structure member and add the length of the padding zeroes where they are "needed", you'll see that the total length is 20.
Reply With Quote
  #5  
Old 08-19-2005, 17:53
infern0 infern0 is offline
Friend
 
Join Date: Dec 2004
Posts: 72
Rept. Given: 4
Rept. Rcvd 0 Times in 0 Posts
Thanks Given: 0
Thanks Rcvd at 2 Times in 2 Posts
infern0 Reputation: 0
Quote:
Originally Posted by tom324
Try with

#paragma pack(1) (or Intel 8.0 equivalent) directive to have alignment set to 1.

Tom
the best way is:
#pragma pack(push, 1)
struct ...
{};
#pragma pack(pop)

to avoid pack problems with other structures
Reply With Quote
  #6  
Old 08-19-2005, 19:11
JuneMouse
 
Posts: n/a
could be a good reading
http://blogs.msdn.com/larryosterman/archive/2005/04/07/406252.aspx
there is a part 2 too
Reply With Quote
  #7  
Old 08-20-2005, 06:29
oxagen
 
Posts: n/a
Quote:
Originally Posted by infern0
the best way is:
#pragma pack(push, 1)
struct ...
{};
#pragma pack(pop)

to avoid pack problems with other structures
Another way
#include <PshPack1.h>
struct ...
{};
#include <PopPack.h>
Reply With Quote
Reply

Thread Tools
Display Modes

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
When use "vendor defined encryption routines", how to set daemon related part? bridgeic General Discussion 6 01-22-2015 11:35
Wlscgen: Are "Vendor Id" and "Developer Id" different ? Numega Softice General Discussion 6 02-12-2007 18:12


All times are GMT +8. The time now is 15:59.


Always Your Best Friend: Aaron, JMI, ahmadmansoor, ZeNiX, chessgod101
( 1998 - 2024 )