We help IT Professionals succeed at work.

struct size in Borland C++ 3.0

Frost_Byte asked
Medium Priority
Last Modified: 2010-08-05
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 ?
Watch Question

That is because of padding.  The borland compiler added an extra byte of padding to make the length divisible by 4.
Unlock this solution and get a sample of our free trial.
(No credit card required)


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 ?


If it's not too much to ask, plz post the ans to the q in the comment as a comment to this thread

#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.

>> 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.  


Thank you very very much
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.