Solved

struct size in Borland C++ 3.0

Posted on 1998-08-09
7
658 Views
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 ?
0
Comment
Question by:Frost_Byte
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1169809
That is because of padding.  The borland compiler added an extra byte of padding to make the length divisible by 4.
0
 
LVL 22

Accepted Solution

by:
nietod earned 30 total points
ID: 1169810
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
 

Author Comment

by:Frost_Byte
ID: 1169811
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Frost_Byte
ID: 1169812
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
 
LVL 22

Expert Comment

by:nietod
ID: 1169813
#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
 
LVL 22

Expert Comment

by:nietod
ID: 1169814
>> 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
 

Author Comment

by:Frost_Byte
ID: 1169815
Thank you very very much
Frost_B
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question