-1
double** ptr_d = new double*[2];
ptr_d[0] = new double[50];
ptr_d[1] = new double[50];

What does this actually do? Im a bit confused

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
zenarthra
  • 17
  • 4
  • `What does this actually do?` Allocating memory? – DimChtz Apr 28 '18 at 22:04
  • Confused about what? Please be more descriptive. – Brian Rodriguez Apr 28 '18 at 22:05
  • My currently understand is: TemperatureValues stored on the stack, is a pointer to the start of an array of (type pointers) to double variables. TemperatureValues is accessing the array using the pointer and allocation an array of 50 doubles – zenarthra Apr 28 '18 at 22:16
  • If you are confused about dynamic allocation of an 2D array the C++ way, check this [**link**](https://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new). Next time before you post a question, I encourage you to look up whether a similar issue already exists. – mareck_ste Apr 28 '18 at 22:16
  • Mareck, apologise i didn't know how to describe the problem – zenarthra Apr 28 '18 at 22:18
  • Thanks ive got the answer form the link! – zenarthra Apr 28 '18 at 22:22

2 Answers2

2

What does this actually do?

The first line creates array of 2 pointers to double. Second and third lines create array of 50 doubles each and assign pointers to the 1st element to 2 pointer array elements:

{double *, double *} //2 pointers to double
    |          |
    |          V
    |      {double1, double2, ...double50} //50 doubles
    |
    V 
{double1, double2, ...double50} //50 doubles
Killzone Kid
  • 6,171
  • 3
  • 17
  • 37
1

The first line allocates an array of pointers to 2 double variables. The next 2 lines fill in the 2 array slots. It would be easier to understand if it had better naming:

double** temperatureValues = new double* [ 2 ];
temperatureValues [ 0 ] = new double [ 50 ];
temperatureValues [ 1 ] = new double [ 50 ];

Or something like that.

You can think of this as a 2 dimensional array of 2 x 50 values. To access one of the values you can do:

double nextTemp = temperatureValues [ 0 ][ 32 ];
user1118321
  • 25,567
  • 4
  • 55
  • 86
  • My currently understand is: TemperatureValues stored on the stack, is a pointer to the start of an array of (type pointers) to double variables. TemperatureValues is accessing the array using the pointer and allocation an array of 50 doubles – zenarthra Apr 28 '18 at 22:14
  • Wouldn't an array of pointers would be: double** temperateValues[]? – zenarthra Apr 28 '18 at 22:16
  • 1
    No. `double** temperatureValues[]` would be an array of pointers to pointers to doubles. Or a 3D array of doubles. – user1118321 Apr 28 '18 at 22:18
  • Thanks for your help, ive got an answer from a link provided! – zenarthra Apr 28 '18 at 22:22
  • Last question, is double*** temperatureValues = double** temperatureValues[]? – zenarthra Apr 28 '18 at 22:30
  • Yes it is. In C arrays and pointers are pretty much the same thing. – user1118321 Apr 28 '18 at 22:32
  • 2
    Though you may think of it as a 2 dimensional array, it is not. This matters a lot performance-wise. That's 3 allocations instead of one, most likely not contiguous, element access requires an indirection, and the compiler has to assume memory areas may overlap, disabling optimisations for correctness. Even for the sake of simplicity, I don't think telling an array and a set of pointers are the same thing is a good idea. – spectras Apr 29 '18 at 01:23
  • @user1118321: `double** temperatureValues[]` can be used everywhere `double*** temperatureValues` is needed (and means the same thing in most cases, e.g. function prototypes), but it's *not* the same thing. The latter requires a declared size or contents and consists of an array (typically backed by stack or global memory, no need to allocate the array itself) storing double-pointers to `double`. The former is a triple-pointer to `double` that behaves similarly, but even the first layer must be initialized to point to memory "elsewhere" (existing stack/global, or allocated on heap). – ShadowRanger May 02 '18 at 18:22