Solved

malloc and padding

Posted on 2013-11-22
3
344 Views
Last Modified: 2013-11-24
Hi

I have a question - If the memory I am reading back seems to be padded to make it aligned to byte boundaries, who is responsible to make sure that sufficient memory was allocated when the writing  took place initially.

For example,, i wrote a series of structs. When i read back i allocated number_of_struct*sizeof(struct mystruct) using malloc. However on looking through the memory returned to me, i find it padded. Was i supposed to allocate  number_of_struct*(sizeof(struct mystruct)+padding) using malloc or is the extra memory allocation handled by the compiler?

thanks
0
Comment
Question by:LuckyLucks
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 39669958
No, the compiler handles this. All compilers have options to control that padding/alignment to improve memory access performance. That is

/Zp[n] pack structs on n-byte boundary for VC++
-fpack-struct[=n] pack structs on n-byte boundary for gcc/g++ and all compatible compilers

There are also compiler-specific #pragma statements to control that:

#pragma pack([show]|[push|pop] [,identifier],n ) for VC++ (http://msdn.microsoft.com/en-us/library/vstudio/2e70t5y1.aspx)
#pragma pack(n) for gcc/g++ and all compatible compilers http://gcc.gnu.org/onlinedocs/gcc/Structure_002dPacking-Pragmas.html)
0
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 39669972
First of all, you're question should become mute if you use the C++ malloc operator known as 'new'.  If you need an array of mystruct, just simply create a new array...

mystruct* p = new mystruct[5]; //Creates 5 new 'mystruct' object in an array

Otherwise, I think so that pointers and arrays would work... the sizeof() operator should account for any padding that goes into a 'mystruct' object.  Therefore you could alternately do the following:
mystruct* p = malloc( sizeof( mystruct ) * 5 );
That should get you the memory you need.  But your structure will not be initialized.  By contrast, if you use the 'new' operator, the constructor for the 5 mystruct objects will get executed.
0
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 39669992
BTW, 'delete' is the opposite of 'new', and if you have an array, you don't delete each object in the array, you simply use the delete array syntax:

delete[] p; //Executes the destructor for each object in the array
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

911 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

20 Experts available now in Live!

Get 1:1 Help Now