Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ objects contiguous in memory?

I am not very good at C++ and I currently porting some piece of code to C. It allocates a big array of objects like this:

o_list::Node** nodes = (o_list::Node**)malloc(SIZE*sizeof(o_list::Node*));

and then fills each position with:

for(int u=0;u<SIZE;u++)
   nodes[u] = new o_list::Node();

From what I understand, the only assurance we have regarding contigous memory is that the pointers to the objects are in fact contiguous, i.e. it might happen that we have:

 _____________________
|x||x||x||x||x||x||x|x| -> contiguous array of pointers;
 |   \ \         
 |    \ \______
 |     \       \
 O      O       O       -> not contiguous positions of objects;

To correct this, I tried to malloc the first array (of pointers) and then malloc an array of objects, and then assign the pointers to the right positions, like this:

o_list::Node** nodes = (o_list::Node**)malloc(SIZE*sizeof(o_list::Node*));

o_list::Node* contObjs = (o_list::Node*)malloc(SIZE*sizeof(o_list::Node));

for(int u=0;u<SIZE;u++){
   contObjs[u] = o_list::Node();
   nodes[u] = &contObjs[u];
}

But this leads to a segmentation fault. I would like to know if my assumption is correct and why I have a segmentation fault in the second option.

Thanks

like image 610
a3mlord Avatar asked Mar 17 '15 11:03

a3mlord


People also ask

Are objects contiguous in memory?

Contiguous objects occupy a contiguous block of memory. The use of contiguous objects makes it easier to enable optimizations that depend on the memory layout of the objects. An object is contiguous if it meets one of the following requirements: It is an object that has the CONTIGUOUS attribute.

What is contiguous in memory?

What is contiguous memory? Consecutive blocks of memory allocated to user processes are called contiguous memory. For example, if a user process needs some x bytes of contiguous memory, then all the x bytes will reside in one place in the memory that is defined by a range of memory addresses: 0x0000 to 0x00FF.

Are arrays contiguous in memory C?

An array is a contiguous memory location which holds data. The data can be integers, characters, floating point numbers, structures etc. Each array element takes a distinct memory location. They all have distinct address.

Are C structs contiguous?

Because the contents of a struct are stored in contiguous memory, the sizeof operator must be used to get the number of bytes needed to store a particular type of struct, just as it can be used for primitives.


1 Answers

You're on the right lines, though getting rid of that first array entirely would be better (if you can). It may involve some design changes elsewhere in your program, so let's leave that for now.

As for your segmentation fault, the only problem I can see is if SIZE is not equal to 300000, you will go out of bounds. Why not use SIZE in the loop?

like image 74
Lightness Races in Orbit Avatar answered Oct 24 '22 15:10

Lightness Races in Orbit