Hey guys! Take a look at the code below:
Do I need to include the line with the pointy arrow?Code:char* c; c = "asdf"; delete[] c; //<-----
Hey guys! Take a look at the code below:
Do I need to include the line with the pointy arrow?Code:char* c; c = "asdf"; delete[] c; //<-----
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
Don't delete anything you didn't new.
no and you also need to fix the code too....
look
char* p; // give me a pointer (could point anywhere)
c="asdf"; // note this is assignment not initialization. You cannot assign strings this way.You need to do this...
strcpy(p,"asdf");
now this has brought us a new problem. We have just written onto memory we dont own.
one quick fix is to turn assignment into initialisation by doing this...
char* p="asdf";
Now you have a pointer to a statically allocated string. You do not need call delete or delete[] on it because the memory used was controlled by the compiler and not you with new or new[].
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
no. delete [] is for dynamically allocated arrays created with a call to new. if anything for that you might use free(c)
personally i dont like static strings, because they're static .
what if you want to change them?
use globalalloc() or malloc(), its much more versatile.
[email protected]
Microsoft Visual Studio .NET 2003 Enterprise Architect
Windows XP Pro
Code Tags
Programming FAQ
Tutorials
You can assign static strings this way. The important
thing is that you never try to modify them.
Ok, thanks But I have no problem with static allocated strings, 'cuz it's for a help thing You know, go to help then controls.
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
if you use [] with your new then use [] with the delete.
example.
char *string=new char[20];
delete [] string;
CEmployee *emp=new CEmployee;
delete emp;
Best Regards,
Bonkey
Ok But that wasn't my question; I know that perfectly well.
But what happens when I do this? (well, someone already answered, but you can ponder my original post anyways.)
Oh yeah, stonedcoder... What do you mean by "you can't assign strings this way"? That's how I had it in my code, and it compiled/ran fine. Unless, of course, you mean that I've written onto memory I don't own, like you said with strcpy() Could you clarify a bit?Code:char* c; c = "asdf"; delete[] c;
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
>But what happens when I do this?
It will behave weird. If you are lucky, it crashes your app.
Reasons are listed above.
hth
-nv
She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."
When in doubt, read the FAQ.
Then ask a smart question.
Hmm, so I've heard. But I recall seeing on another thread (I think it was in the C programming forum) Fordy recommending this method to some other guy... a slip of mind? (And besides, it seems to work fine for me! )
Or maybe I just remembered wrong...
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
Yeah, but I certainly didnt call delete on it!Originally posted by Hunter2
Hmm, so I've heard. But I recall seeing on another thread (I think it was in the C programming forum) Fordy recommending this method to some other guy... a slip of mind? (And besides, it seems to work fine for me! )
Or maybe I just remembered wrong...
It may work now for you, but in time could blow up in your face.
To clarify a little on one point raised here (not related to delete)... this
is perfectly valid. A use of it might be:Code:char* c; c = "asdf";
Of course, you can't change the contents of the string literal at run time though.Code:char *GetErrorText(int ErrorCode) { char *p; switch (ErrorCode) { case 1: p = "Open failed"; break; case 2: p = "Close failed"; break; default : p = "Unknown error"; break; } return p; }
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
goto is perfectly valid, too. But it is not recommended.Originally posted by Hammer
is perfectly valid. A use of it might be:Code:char* c; c = "asdf";
Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling
>goto is perfectly valid, too. But it is not recommended.
Different kettle of fish altogether...
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]