Do I need to delete[] this?

This is a discussion on Do I need to delete[] this? within the C++ Programming forums, part of the General Programming Boards category; Hey guys! Take a look at the code below: Code: char* c; c = "asdf"; delete[] c; //<----- Do I ...

  1. #1
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879

    Do I need to delete[] this?

    Hey guys! Take a look at the code below:
    Code:
    char* c;
    c = "asdf";
    delete[] c;   //<-----
    Do I need to include the line with the pointy arrow?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  2. #2
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Don't delete anything you didn't new.

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    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

  4. #4
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    no. delete [] is for dynamically allocated arrays created with a call to new. if anything for that you might use free(c)

  5. #5
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    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.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  6. #6
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    You can assign static strings this way. The important
    thing is that you never try to modify them.

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    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.

  8. #8
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    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

  9. #9
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    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.)

    Code:
    char* c;
    c = "asdf";
    delete[] c;
    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?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  10. #10
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,138
    >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.

  11. #11
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    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.

  12. #12
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    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...
    Yeah, but I certainly didnt call delete on it!

    It may work now for you, but in time could blow up in your face.

  13. #13
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    To clarify a little on one point raised here (not related to delete)... this
    Code:
    char* c;
    c = "asdf";
    is perfectly valid. A use of it might be:
    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;
    }
    Of course, you can't change the contents of the string literal at run time though.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  14. #14
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by Hammer

    Code:
    char* c;
    c = "asdf";
    is perfectly valid. A use of it might be:
    goto is perfectly valid, too. But it is not recommended.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  15. #15
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >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]

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. delete[]
    By R.Stiltskin in forum C++ Programming
    Replies: 23
    Last Post: 11-24-2008, 04:26 PM
  2. delete[] problem with release config...
    By mikahell in forum C++ Programming
    Replies: 8
    Last Post: 08-21-2006, 10:37 AM
  3. delete[] or delete?
    By X PaYnE X in forum C++ Programming
    Replies: 7
    Last Post: 03-30-2005, 02:16 PM
  4. Memory issue with new[] and delete[]
    By Zarkhalar in forum C++ Programming
    Replies: 24
    Last Post: 08-07-2004, 07:45 AM
  5. delete and delete[]
    By Hunter2 in forum C++ Programming
    Replies: 13
    Last Post: 06-26-2003, 04:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21