2

For some reason this is giving me more trouble than i thought...

int *myArray[3];

myArray = new int[mySize];

does not work...

I've used a typedef before in a similar manner and it worked perfectly, but this time i dont want to create the typedef

1
  • 2
    It's not clear what you're trying to do. Do you mean myArray[0]=new int[mySize];? Commented Dec 16, 2011 at 3:18

3 Answers 3

6

One might be tempted to do this:

::std::vector<int[3]> myArray;

Because vector is so nice for dynamically sized arrays. Unfortunately, while that declaration works, the resulting vector is unusable.

This will be just as efficient, if you have ::std::array (a C++11 feature) and it will actually work:

::std::vector< ::std::array<int, 3> > myArray;

If you can do this, I would highly recommend it. vector is much nicer and safer to deal with than an array you have to allocate yourself with new.

Otherwise, try this:

typedef int inner_array_t[3];
inner_array_t *myArray = new inner_array_t[mySize];

And since you don't want to use a typedef for some odd reason, you can unwrap it like so:

int (*myArray)[3] = new int[mySize][3];
Sign up to request clarification or add additional context in comments.

6 Comments

It should be pointed out that c++ vectors have little to no overhead on the top of an array (a small amount of memory overhead, I believe access times remain the same). Unless you are interfacing with C code or something else that requires arrays, it's almost always better to use STL vectors.
@ShaunBouckaert: Even if you are interfacing with C code, there are ways to get a pointer that can be passed to C code in a pinch. You just have to make sure the vector isn't modified while the C code is executing.
I don't think std::vector<int[3]> myArray; actually works, because C style arrays do not fulfill the requirements of a vector. What would you push_back into the vector? Something like int a[3]; myArray.push_back(a); doesn't work.
@Fred I am sure you are right, even though vector can re-allocate memory to expand to fit more arrays the size of the array is to hold is non deterministic. Though, could you not have a vector of pointers to arrays, the pointers would be a fixed size
@thecoshman: Or you could have a std::vector<std::array<int, 3>>.
|
6
int (*myArray)[3] = new int[mySize][3];

?

Comments

3
int *myArray[3];

This means "myArray shall be an array of three pointers-to-int".

You presumably wanted "myArray shall be a pointer-to-(array of three ints)". That is spelled int (*myArray)[3].

This sort of thing is much easier with typedefs.

typedef int datum[3];
datum* myArray = new datum[mySize]; // no fuss, no muss.

But seriously, just use std::vector. And make an actual struct for your group-of-three-integers. Or, if it really should behave like an array, use boost::array so that you at least get the behaviour of a first-class type.

Comments

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.