Solved

struct size in Borland C++ 3.0

Posted on 1998-08-09
7
656 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
  • 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
Industry Leaders: 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

Suggested Solutions

Title # Comments Views Activity
C++ question 3 74
Why isn't object file created? 6 86
How to read a c++ line with strings and variable numbers of elements 6 44
Adding items to a C# list incrementally 5 58
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

680 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