Solved

malloc and padding

Posted on 2013-11-22
3
350 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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 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.

786 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