I'm seriously confused. When I asked for help with making a two dimensional array of the free store, some people gave some method using a loop that I didn't understand. But when I was messing around with pointers, I discovered that I could make a perfectly functioning two dimensional array on the free store very, very easily:

Code:

char * foo[3] = {new char[3]};

What's going on? Why don't people just use this method :confused:

11-07-2003

Prelude

>Why don't people just use this method
Because you have an array of three pointers to dynamic arrays. In other words, you can resize any of the rows, but you can't add any more. You're stuck with three. Sometimes this is what you want, but most of the time it isn't, that's why you don't see it very much.

Code:

char * foo[3] = {new char[3]};

This creates an array of three pointers to char and initializes the first of them with dynamic memory, the other two are initialized to NULL. So don't be surprised if your program segfaults when you try to use foo[1] and foo[2].

11-07-2003

thefroggy

That array is not on the free store-- it's on the stack. It creates an array on the stack of 3 pointers to char.

In fact, according to gcc3.2, the code doesn't work as you would expect. I compiled this program: