Doubt about delete keyword and char*

Dec 13, 2010 at 1:02pm
Hi,
In the code snippet below, the program gets crashed at delete operator.What would be the possible cause, after doing lots of modification with this code. I had guessed that char* are managed by the compiler and there's no need for memory allocation.
But I didn't find any reason to this thought. Please let me know what the actual thing is going behind the scene.
Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
void deleteKeyword() {
    for(int i = 0; i < 5; i++) {
        char *tmpCString = NULL;
        tmpCString = new char;
        tmpCString = "vivek Kumar\n";
        cout << tmpCString;
       if( tmpCString != NULL) {
           delete tmpCString;
           tmpCString = '\0';
	}
    }
}
Dec 13, 2010 at 1:21pm
Don't guess, but reread the chapter about memory allocation in your book. In the meantime, use std::string.

tmpCString = "vivek Kumar\n"; is not valid C++, because "vivek Kumar\n" is a string literal, i.e. a constant char array. You cannot cast it to a pointer to non-const char. Even if tmpCString were a pointer to const char, you'd have a memory leak here, because you're manually creating a char using new, but then overwrite the address with that of the string literal.
Next, delete tmpCString; is also incorrect. You may never use delete on a string literal (or any other address that was not returned by the new operator). To delete an array (that was created with new[]) you need to use operator delete[] anyway.
tmpCString = '\0'; makes no sense either. '\0' is a character, but tmpCString is a pointer. This still works, because '\0' can be converted to a null pointer, but you should write tmpCString=0; or tmpCString=NULL; in the first place, which is common practice.
Dec 13, 2010 at 1:30pm
why don't u use int* to understand pointers... pointers only store addresses... n u r assigning a value to it...
1
2
3
4
int *tmpCString = new int[10];
// here tmpCString is pointing to dynamic int[0]...
       
           delete[] tmpCString;


try this...
Dec 13, 2010 at 1:57pm
@Athar

Thanks for pointing me out that tmpCString = "vivek Kumar\n"; is not a c-string rather its a const char array.
But if you dont mind I would you to know is it right to use the below code as I'm still having doubt about this.

1
2
3
4
 
char * someCharPtr = "something" ; // this true as it's a initialization

someCharPtr = "something new again"; 


so while assigning someCharPtr for second time again, is this right thing.
Dec 13, 2010 at 2:26pm
Sorry I have again did one mistake in my last post
 
char * someCharPtr = "something" ;

as i mean
 
char  someCharPtr[] = "something" ; // this is correct initialization. 


but what's actually the compiler do the code below
1
2
char * someCharPtr = "something" ; 
someCharPtr = "something new again";


Dec 13, 2010 at 3:25pm
someone please help me.
Dec 13, 2010 at 3:33pm
In this:
char someCharArray[] = "something";
someCharArray is not a pointer, it's a char array.
The code is equivalent to
char someCharArray[10]={'s','o','m','e','t','h','i','n','g','\0'};

You cannot assign C arrays, so
someCharArray="something new again";

won't work.

That's why you should use std::string:
1
2
std::string str="something";
str="something else";

works just fine.
Last edited on Dec 13, 2010 at 3:36pm
Dec 13, 2010 at 4:30pm
Thanks a lot u all!
Topic archived. No new replies allowed.