• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 679
  • Last Modified:

struct size in Borland C++ 3.0

I made this declaration :
struct test {
char a,b,c;
};
For some reason, when using the sizeof function (sizeof(test))  I get 4 bytes instead of the expected 3 (well, I expected there to be 3...one for each char and thats it). In fact I checked this out using pointers and there does seem to be an extra byte there. This does not happen with MS-VC++ 4.2, but I'm trying to figure out why it does with Borland. Any suggestions ?
0
Frost_Byte
Asked:
Frost_Byte
  • 4
  • 3
1 Solution
 
nietodCommented:
That is because of padding.  The borland compiler added an extra byte of padding to make the length divisible by 4.
0
 
nietodCommented:
If you do
#pragma pack(1)
struct test {
   char a,b,c;
   };

the pragma should switch the compiler to byte alignment and the size will be 3.

0
 
Frost_ByteAuthor Commented:
can U elaborate - is the declaration :
#pragma pack(1)
some sort of compiler derective ? is there some switch I can set in the Borland env so this wont happen again ? will this apply to all future declarations in the same program ?

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Frost_ByteAuthor Commented:
If it's not too much to ask, plz post the ans to the q in the comment as a comment to this thread
Thanks
0
 
nietodCommented:
#pragma's are "directives" that can be placed inside your source code to control the compiler.  They are compiler-specific, they are not part of the C++ standard language.  The existence of pragma's are part of the standard, that is, the standard says that compilers can create and recognize any pragmas they want.  However, the standard doesn't say what pragma are to be supported or even what hey do.  That is, just because one compiler recognizes, "#pragma pack()" does not mean that another will.  Or one compile will interpret it as one thing and another as something else.

Thus, if you use pragma's you code may be less portable, since the pragma may not work on other compilers.  

If you search the Borland help for "#pragma" you will find this an other pragma's documented.  The typical way this pragma would be used would be to push the current allignment (saving it because you might not know what it is and it might be important for other code.)  Then set the allignment how you want it, then define your structure or class that needs specific alignment, then pop the saved allignment, so that other code is compiled with the previous alignment setting, like

#pramga pack(push,1) // push packing and set to 1.
// define structure.
#pramga pack(pop)  // restore previous packing.

This is in the docs as well.

You can use a command line option to set the initial packing (allignment) value.  Thus you could set it to 1.  However, it might not stay at 1 as you might include some code that alters the packing (the window.h files do, for example)  Thus, I find that the pragma's are better for controlling this.
0
 
nietodCommented:
>> will this apply to all future declarations in the same program ?
What happens is that the allignment starts at some default value, probably 4.  Iif you specify a new alignment in the command line options, then it changes to that alignment.  then the compiler goes through the code and creates the structures using the "current" alignment.  Whenever the compiler encounters a #pragma pack() it changes the "current" allignment.  This will affect the new structures and classes it comes to until it reaches a new #pragma pack() directive.  
0
 
Frost_ByteAuthor Commented:
Thank you very very much
Frost_B
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now