?
Solved

malloc and padding

Posted on 2013-11-22
3
Medium Priority
?
387 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
[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
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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…
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.
Suggested Courses

765 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