help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

allocate 2d pointer. is ths a bug ?


From: h
Subject: allocate 2d pointer. is ths a bug ?
Date: Thu, 10 Aug 2006 10:09:58 +0900

Hi,

i've tested 2d pointer on VC++ 6.0 SP5 and g++ on Cygwin and g++ on Ubuntu 
6.06.

look,

char** str;
str[0] = new char[5];

i know above code are wrong.
The result is "error" build on VC++ 6.0.
BUT, g++ on Cygwin is not occur any warning at least. (different on Ubuntu.)

On Ubuntu, i had build two ways.
first, just only using "str[0]" one; build and execute then occur 
"Segmentation fault".
but, i'm trying "str[0]" and "str[1]" together; build and execute then 
doesn't occur any problem.
and then, re-trying 1st one, result is clear(doesn't occur any problem).
i don't know what's wrong anyway.

is this the g++ bug ? i've attached my test code below. check it.

Regards,

godmode2k at hotmail.com
from KLDP.org


// -------------------------------------------------

Tested on:

<Cygwin>
$ g++ --version
g++ (GCC) 3.4.4 (cygming special) (gdc 0.12, using dmd 0.125)

<Ubuntu 6.06 Dapper Drake>
g++ 4.0.3

// -------------------------------------------------

<code>
1 #include <iostream>
2
3 using namespace std;
4 int main(void) {
5    char** str;
6
7    str[0] = new char[5];
8    str[1] = new char[5];
9
10    memset( str[0], 0x00, sizeof(str[0]) );
11    strncpy( str[0], "test", 5 );
12
13    memset( str[1], 0x00, sizeof(str[1]) );
14    strncpy( str[1], "best", 5 );
15
16    cout << "str[0] = " << str[0] << endl;
17    cout << "str[1] = " << str[1] << endl;
18
19    if( str ) {
20        for( int i=0; i<2; i++ )
21            delete [] str[i];
22
23        cout << "deleted..." << endl;
24    }
25
26    return 0;
27 }
</code>

// -------------------------------------------------

<result>
$ ./test
str[0] = test
str[1] = best
deleted...
</result> 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]