Reputation: 613
So I've been trying to figure this out for a while, but I just can't wrap my head around it.
If I have an array like this:
int numbers[] = {0,1,2,3,4,5,6,7,8,9};
each number will be stored in consecutive memory with a distance of an int (4 bytes on my system) between eachother. So if I have an array of pointers like this:
int* pNumbers[] = {new int(0), new int(1), new int(2)};
I expected them to be stored in memory with a distance of a pointer (4 bytes on my system) between eachother, but that isn't the case. Instead they are stored with a distance of 64 bytes. Also if I create an array consisting of pointers to a Struct containing an int and a bool the distance is 72 bytes.
Can someone explain what is happening here and how the distance between each element in the array is determined.
Upvotes: 1
Views: 1323
Reputation: 70333
int* pNumbers[]
This declares an array of consecutive int *
elements. They will be exactly sizeof( int * )
bytes apart. Depending on platform, that will likely be somewhere between 2
and 8
, but most definitely not 64
.
... = {new int(0), new int(1), new int(2)};
This initializes the elements with pointers to allocated memory, initializing the memory with the given values. Depending on your implementation, those pointers to the memory could be as close together as sizeof( int )
, far apart, or non-contiguous. Exact numbers are unspecified.
So, the int *
elements in the array will be contiguous and exactly sizeof( int * )
apart. They could point just about anywhere.
That is why commentators (myself included) suspected a problem with your measurement, i.e. you printing the addresses pointed to, not the addresses of the pointers.
Upvotes: 4
Reputation: 171383
You made the wrong observation.
For the array of int
you are correct that:
each number will be stored in consecutive memory with a distance of an int (4 bytes on my system)
Similarly, for the array of pointers each pointer will be stored in consecutive memory with a distance of sizeof(int*)
between them. That's how arrays work.
I assume what you looked at was the value of the pointers, but you should have looked at their addresses.
In the int
array case you didn't look at the values of the array elements (those are just integers with values that increase by 1
, because that's what you stored in the array) you looked at the addresses of the elements.
You need to do the same for the array of pointers. Don't look at the values of the pointers (those are addresses on the heap where new
allocated memory for each int
, because that's what you stored in the array), look at the addresses of the pointers.
The values of the heap pointers could have fairly arbitrary values, depending on the implementation details of malloc
on your system. You happen to observe that they are 64 bytes apart, but that is not guaranteed, and you can't rely on it. If you had a more complicated program that performed other heap allocations and deallocations you might find a completely different pattern, rather than three values that increase by a fixed amount each time.
Upvotes: 1
Reputation: 2181
This is by design. When you manually allocate memory from the heap, the C++ runtime provides a memory manager (on top of the Operating System's memory manager), and makes no guarantees where it gets the memory that you allocate.
However, in modern C++, you really want to avoid allocating memory whenever possible. First, dynamically allocating int's is insanely expensive since each 4-byte int will require 8 bytes for the pointer in 64-bit mode.
Instead, for a dynamic array of int, use std::vector. The Standard Library offers a lot of flexibility, and most/all of the overhead will be compiled out in such simple cases.
Upvotes: -1
Reputation: 7709
If you call "new int(0)" you tell the operation system (windoes, linux, osx etc.) to give you 4 bytes (sizeof(int)) of data, where you can store your integer. The operation system is free to give you any space in memory it feels like.
Why does your OS give you memory points 64 bytes apart I can not tell.
The results should vary depending on which system you are running the test.
Upvotes: -1