Solved

malloc and padding

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SetCurrentDirectory path limit 7 130
How to convert MFC::CString to UTF8 wchar_t* 10 276
c++, dynamic object by json 1 53
designing in object programming 12 94
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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

790 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