On 5/16/07, mahesh_anasuri <[EMAIL PROTECTED]> wrote:
>
> int main()
> {
> char *str = new char[12];
> delete [] str;
>
> char *test = new char[12];
> strcpy(str, "surprise !");
> cout << test << str;
> delete [] test;
>
> }
>
> What is the reason behind same o/p for both pointers, though test is
> not copied with any data?
>
>
>
Hi Mahesh,
I modified your code a little as,
int main()
{
char *str = new char[12];
delete [] str;
char *test = new char[12];
cout <<(void*) test <<":"<< (void*)str<<endl;
strcpy(str, "surprise !");
cout << test << str;
delete [] test;
}
Now the addresses printed are as:-
0x00332D58:0x00332D58
surprise !surprise !
And if I comment the line
delete [] str;
in code,
i get output as,
0x00332D70:0x00332D58
►♦3surprise !
It seems that delete[] frees up the pointer for later use which is grabbed
by next new char[] call.
And then both poiinters point to same location. delete[] only frees up the
memory allocated and not the pointer iteself.
-Abhishek
[Non-text portions of this message have been removed]