-3

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!

yierstem
  • 1,933
  • 5
  • 21
  • 42
  • 1
    It sounds like you just want an array of `double`s (`double dp[10]`). Though I'd suggest a [`std::array`](https://en.cppreference.com/w/cpp/container/array) instead. You only use pointers when you need to refer to some other `double` that exists somewhere else. – François Andrieux Oct 17 '18 at 19:36
  • you want an array of double or an array to pointers on double? – OznOg Oct 17 '18 at 19:37
  • 1
    The scope of question is way too broad in my view. I suggest you read one of the [good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – SergeyA Oct 17 '18 at 19:37
  • 1
    You should provide a [Minimal Complete Verifiable Example](https://stackoverflow.com/help/mcve). The code shown in the question does not match the behavior you describe. – Eric Postpischil Oct 17 '18 at 19:49
  • 1
    The compiler does not automatically create a target for your pointers because you might assign them to point to already existing storage. So after creating a pointer you also have to create the double and assign its address to the pointer. Your second code is correct. – stark Oct 17 '18 at 20:12
  • bad idea my opinion but (may be) you could do it with `std::vector t(10); t.emplace(..., new double(0.0))` – Алексей Неудачин Oct 17 '18 at 21:17

2 Answers2

1

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.

Acorn
  • 24,970
  • 5
  • 40
  • 69
0
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.

SoronelHaetir
  • 14,104
  • 1
  • 12
  • 23