'new char' issue

Hi there,
For example char aa[100] -> char *aa=new char[100];
But for char aa[100][100] how to do this in the same way? char *aa=new char[100][100] isn't good.
1
2
char ** aa = (char**)new char[100][100];
// or char (*a)[100] = new char[100][100]; 
Last edited on
closed account (zb0S216C)
Syuf, you forgot the second part. Shouldn't it be:

1
2
3
4
5
6
7
8
9
10
char **aa = ( new( std::nothrow ) char* [100] );

for( short i( 0 ); i < 100; i++ )
    aa[i] = ( new( std::nothrow ) char[100] );

// Deletion:
for( short i( 0 ); i < 100; i++ )
    delete [] aa[i];

delete [] aa;


Wazzak
Last edited on
Thank you guys.
Syuf, you forgot the second part.

I don't understand you. What are you about? Deletion? I didn't forget, I thought that author will do it by hisself.
Some comments about your code:
Question: why there are these useless brackets? Isn't operator = more clear to understand? I think they make the code heavy.

Also it doesn't even compile:
char **aa = new (nothrow) char[100] : result of new char[100] will be char*, but you assign it to char** variable. (maybe it is typo)

for(short i(0); ...) : why short? default array indexes are not short signed int, they are ptrdiff_t!

Why do you use new(std::nothrow)? I think in small examples it is useless.
One more thing, why i can not use it with a iteration? like aa[i] ?-
1
2
3
char **a=(char**)new char[10][100], b[10][100];
strcpy(b[0],"john"); strcpy(b[1],"siloan");
for() strcpy(a[i],b[i]);

And also when i'm doing this
 
char **a=(char**)new char[10][100]; strcpy(a[1],"john");

the program crashes.
That is why the green variant (in comments) is better.
It is because a[i] == *(a + i*sizeof base), where base is int* instead of int [100].
If you want the first variant you need to write smth like this:
strcpy( ( (int (*)[100]) a) [i], b[i]);
Hm ok, thanks again.
closed account (zb0S216C)
Syuf wrote:
why there are these useless brackets? Isn't operator = more clear to understand? I think they make the code heavy. (sic)

The outcome is the same. The style falls down to personal preference. It's only one set (2 sets if you count those wrapped around std::nothrow); it's far from heavy. It's not like the compiler cares anyway.

And yes, my code has a typo or two (I'll correct them after this post).

Syuf wrote:
why short? (sic)

Why push 4 bytes onto the stack when I can comfortably push 2 bytes onto the stack with the same outcome?

Syuf wrote:
default array indexes are not short signed int, they are ptrdiff_t! (sic)

True, but I wasn't performing pointer arithmetic.

Syuf wrote:
Why do you use new(std::nothrow)? I think in small examples it is useless. (sic)

It's cleaner than the try ... catch exception handlers.

Wazzak
Why push 4 bytes onto the stack when I can comfortably push 2 bytes onto the stack with the same outcome?

The same outcome will be untill you haven't overflow.
It's cleaner than the try ... catch exception handlers.

And they are not needed there, too.
closed account (zb0S216C)
Syuf wrote:
The same outcome will be untill you haven't overflow. (sic)

100 is way below the maximum of short. Where did overflow come from? No bounds are being exceeded within my example.

Syuf wrote:
And they are not needed there, too.

You're right, they aren't; I was stating a point.

Wazzak
Topic archived. No new replies allowed.