简体   繁体   中英

Allocating array of pointers to doubles

I'm trying to initialize every element of an array of pointers to doubles with the associated value 0.0 . I have the following code:

double* dp[10];
for (int i = 0; i < 10; i++) dp[i] = 0.0;

But this gives me Segmentation fault and I'm not sure how to fix this.

I tried:

for (int i = 0; i < 10; i++) {
    dp[i] = new double; // this seems to fix the segmentation problem.
    *(dp[i]) = 0.0; // accessing the associated value in order to asign a new value
}

But I'm not sure if this is the correct way to do it. Could someone explain me the dynamic memory allocation in detail?

When double* dp[10] is executed and an array of pointers is created. Those pointers: where do they point to in the memory? If anywhere, why can't I just use that place in the memory to store every double? Why do I have to specify new double , isn't it obvious by the type of the pointers?

Sorry if I'm asking stupid questions, but I haven't fully understood (actually at all) how this works.

Thanks in advance!

When double* dp[10] is executed and an array of pointers is created.

That lines does not really execute anything, it just reserves the space (in the stack) for 10 pointers to double .

Those pointers: where do they point to in the memory?

Nowhere, because they aren't initialized. Thus, consider them invalid until they receive some valid value.

why can't I just use that place in the memory to store every double?

Because you asked for 10 pointers to double , instead of 10 doubles .

Why do I have to specify new double, isn't it obvious by the type of the pointers?

Yes, but in C++ new is an expression, independent on its own. C++ could have had something like new(p) , where p is a pointer, but it is not like that :)

Note, however, that you can implement it yourself with templates:

template <typename T>
void allocate(T* & p, std::size_t n)
{
    p = new T[n];
}

Or, in C++11, you can simply use auto if you like it:

auto p = new int[10];

Having said all that, if you already know in advance that you exactly need 10 doubles , you could simply ask for that:

double my_ten_doubles[10];

No need for memory allocation (expensive), no need for memory deallocation (no memory leaks), etc.

double* dp[10];

creates an array of pointer to double, where that array exists in memory depends on whether the array is inside a function or external, but either way it only allocates the array and you cannot count on the individual elements having any particular value let alone count on that value being a usable address.

dp[i] = new double; allocates memory that can store a double and assigns a pointer to that memory to the array element. You can then use *dp[i] = 0.0; to assign a value to the actual memory that was allocated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM