Solved

struct size in Borland C++ 3.0

Posted on 1998-08-09
7
650 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Frost_Byte
Comment Utility
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
Comment Utility
#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
Comment Utility
>> 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
Comment Utility
Thank you very very much
Frost_B
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now