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.

4
  • 1
    This is not an array of pointers. Commented Jun 17, 2013 at 2: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. stackoverflow.com/a/4915125/179910 Commented Jun 17, 2013 at 2:52
  • The declaration for array declares it as a pointer to an array of floats. new float[faces * 3][3] returns a pointer to a multidimensional array of floats which is really organized as a one-dimensional array Commented Jun 17, 2013 at 2:55
  • @JerryCoffin As explained below a library did this to me Commented Jun 18, 2013 at 16:45

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.