0

I have a program that I've written that requires me to declare an array as such

float (*array)[3] = new float[faces * 3][3];

Now I understand the syntax and all, this is an array of pointers to fixed size arrays. What I don't understand is the underlying organization behind this. Since there was only one memory allocation (for the array of pointers) how does the memory for the fixed size arrays get allocated?

Along the same thread, since there was only one allocation there should be one deletion, meaning the array is deleted by

delete[] array;

but I'm confused as to how this gets all of the memory, given that it seems only the array of pointers has been deleted, as opposed to the memory they pointed to.

Pat
  • 331
  • 1
  • 4
  • 16
  • 1
    This is not an array of pointers. – n. m. could be an AI Jun 17 '13 at 02:52
  • 3
    Why do you do this to yourself? I'd advise forgetting that you ever even heard of `new[]`, and use `std::vector` instead. A class to make that act like a 2D array is pretty trivial. http://stackoverflow.com/a/4915125/179910 – Jerry Coffin Jun 17 '13 at 02:52
  • The declaration for `array` declares it as a pointer to an array of `float`s. `new float[faces * 3][3]` returns a pointer to a multidimensional array of `floats` which is really organized as a one-dimensional array – A.E. Drew Jun 17 '13 at 02:55
  • @JerryCoffin As explained below a library did this to me – Pat Jun 18 '13 at 16:45

2 Answers2

1

This is not an array of pointers to fixed size arrays. This is a pointer to a multidimensional array. Multidimensional arrays are implemented as one dimensional array, with some calculation upon accessing elements.

The memory layout is exactly like in this statement:

float *array = new float[(faces * 3) * 3];

or in this one (except faces must be constant expression, and the allocation is now on the stack):

float arr3[faces*3][3];
float (*array)[3] = &arr3; // note the "&". it is not a decaying here

and this is a more familiar form of this pointer:

void something(float array[][3]); // this is not an array, but a pointer to one.

Note that the arrays of different sizes/dimensions are distinct types, and if you want to access a one dimensional array as a multi-dimensinal one, now will need to do the calculation of array[3][2] yourself.

Elazar
  • 20,415
  • 4
  • 46
  • 67
0

Baby steps.

First, these fixed-length arrays of float[3] seem like special types. I'm guessing you'll be doing specific operations with them. You should wrap a float[3] with the functions and operations that will work with them into a class. You may decide to use Vector<float> internally and keep them at size 3, but std::vector was designed to be appended to and removed from. I think there is another "std template" class that was designed for fixed-length multiples, but I don't know which. I don't use the STL much.

Once you've objectified your float[3]'s, however you do it, I think the rest will become easier as you start to see more clearly.

0decimal0
  • 3,884
  • 2
  • 24
  • 39
John
  • 7,301
  • 2
  • 16
  • 23
  • Not to be rude but this doesn't answer the question. I'm working with a library that uses this particular setup. It's also just used for one function, the data is read into what I believe is a more logical setup, and the array is deleted literally 10 lines from where it was created. Doing what you suggested really wouldn't be worth it. – Pat Jun 17 '13 at 03:16