char * string has to be free() or delete []

Discussion in 'C++' started by zalzon, Aug 24, 2004.

  1. zalzon

    zalzon Guest

    When i create a char * string lets say, do i also have to then
    remember to free up the memory after the program is done? Or is it
    done automatically?

    Is this right?

    char * string = "Hello";

    string = NULL;
    free(string);
    zalzon, Aug 24, 2004
    #1
    1. Advertising

  2. zalzon

    Ron Natalie Guest

    "zalzon" <> wrote in message news:...
    > When i create a char * string lets say, do i also have to then
    > remember to free up the memory after the program is done? Or is it
    > done automatically?
    >
    > Is this right?
    >
    > char * string = "Hello";
    >
    > string = NULL;
    > free(string);


    No, it is NOT right. char* isn't a string type. It's a pointer to a single char.
    It is incumbent on you to remember how it was allocated. In the example
    above, it's allocated to a statically allocated string literal. You don't call free
    or delete on it.

    If you alloc'd it with malloc you'd call free.
    If you alloc a single character with new, you free it with delete.
    If you alloc an array of char with new [], you free it with delete [].
    Ron Natalie, Aug 24, 2004
    #2
    1. Advertising

  3. zalzon

    zalzon Guest

    So how would you free it in the above case then if not with free() ?
    zalzon, Aug 24, 2004
    #3
  4. zalzon

    Chris Dutton Guest

    zalzon wrote:
    > So how would you free it in the above case then if not with free() ?


    You don't free it.
    Chris Dutton, Aug 24, 2004
    #4
  5. zalzon

    zalzon Guest

    On Tue, 24 Aug 2004 03:37:41 GMT, Chris Dutton <>
    wrote:

    >zalzon wrote:
    >> So how would you free it in the above case then if not with free() ?

    >
    >You don't free it.



    Ah.

    What happens to the pointer then?

    I'm confused as to when you free and when not to free. I thought
    anytime you create a pointer, you have to remember to free it.
    zalzon, Aug 24, 2004
    #5
  6. zalzon

    Artie Gold Guest

    zalzon wrote:
    > On Tue, 24 Aug 2004 03:37:41 GMT, Chris Dutton <>
    > wrote:
    >
    >
    >>zalzon wrote:
    >>
    >>>So how would you free it in the above case then if not with free() ?

    >>
    >>You don't free it.

    >
    >
    >
    > Ah.
    >
    > What happens to the pointer then?
    >
    > I'm confused as to when you free and when not to free. I thought
    > anytime you create a pointer, you have to remember to free it.


    Any time you _dynamically allocate memory_ it must be freed.

    [in your original example nothing is being dynamically allocated]

    HTH,
    --ag
    --
    Artie Gold -- Austin, Texas

    20050120->44
    Artie Gold, Aug 24, 2004
    #6
  7. zalzon

    zalzon Guest

    On Mon, 23 Aug 2004 23:10:25 -0500, Artie Gold
    <> wrote:

    >Any time you _dynamically allocate memory_ it must be freed.


    So anytime malloc is used, must remember to free.

    Got ya.
    zalzon, Aug 24, 2004
    #7
  8. zalzon

    zalzon Guest

    Sorry one more question please.

    If i malloc() memory to a char * string, do i then do this to free it?

    string = NULL;
    free(string);
    zalzon, Aug 24, 2004
    #8
  9. zalzon wrote:
    >
    > Sorry one more question please.
    >
    > If i malloc() memory to a char * string, do i then do this to free it?
    >
    > string = NULL;
    > free(string);
    >
    >


    Yes.

    malloc goes with free
    calloc free
    realloc free

    new delete
    new[] delete []

    In C++ you usually use new/delete and not malloc/free.
    You also don't allocate memory to a char* string, but
    you use the std::string class.

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Aug 24, 2004
    #9
  10. zalzon

    Rolf Magnus Guest

    zalzon wrote:

    > Sorry one more question please.
    >
    > If i malloc() memory to a char * string, do i then do this to free it?
    >
    > string = NULL;
    > free(string);


    No. Well, yes, you have to free() it, but not the way you do. What
    you're doing is first overwriting the pointer to your allocated memory
    with a null pointer, then calling free on that null pointer. The
    allocated memory is still existing, and you lost your pointer to it, so
    you produced a memory leak. Just leave the "string = NULL;" line out.
    Rolf Magnus, Aug 24, 2004
    #10
  11. In message <>, Karl Heinz Buchegger
    <> writes
    >zalzon wrote:
    >>
    >> Sorry one more question please.
    >>
    >> If i malloc() memory to a char * string, do i then do this to free it?
    >>
    >> string = NULL;
    >> free(string);

    >
    >Yes.


    No. (Those two lines above need to be swapped ;-)

    >malloc goes with free
    >calloc free
    >realloc free
    >
    >new delete
    >new[] delete []
    >
    >In C++ you usually use new/delete and not malloc/free.


    And this is the punchline:

    >You also don't allocate memory to a char* string, but
    >you use the std::string class.
    >

    And for arrays of other types it's usually better to let std::vector
    take care of the allocation for you. It's rare to find new[]/delete[]
    being used for any good reason outside of library code.

    --
    Richard Herring
    Richard Herring, Aug 24, 2004
    #11
  12. zalzon

    tom_usenet Guest

    On Tue, 24 Aug 2004 04:25:41 GMT, zalzon <>
    wrote:

    >On Mon, 23 Aug 2004 23:10:25 -0500, Artie Gold
    ><> wrote:
    >
    >>Any time you _dynamically allocate memory_ it must be freed.

    >
    >So anytime malloc is used, must remember to free.


    Right, remember a pointer is just an address, it's what the pointer is
    pointing to that needs freeing when it was malloced.

    Tom
    tom_usenet, Aug 24, 2004
    #12
  13. zalzon

    Ron Natalie Guest

    "zalzon" <> wrote in message news:eek:...

    > What happens to the pointer then?
    >
    > I'm confused as to when you free and when not to free. I thought
    > anytime you create a pointer, you have to remember to free it.


    You are confused between the concept of a pointer and the value it
    contains.

    Every object in C++ exists someplace. Sometimes that is in automatically
    allocated storage (as a local variable), sometimes statically allocated (a global
    or explicit static), sometimes dynamically allocated (new or malloc), some times
    it's part of a larger object.

    The pointer you used most likely was a auto variable (local to a function). It
    takes care of itself.

    The question is "What about the value you placed in it?"

    This value is a statically allocated character array. It lives for the life of the program.
    There's no need to do anything with it.

    Had you allocated it with malloc or new, then you'd have to deal with it. However
    the fact that it is in the pointer is MEANINGLESS. The pointer is a piece of scratch
    paper that you remember the value on. It's HOW the thing you put into it that matters.


    Further, at this point, I recommend you get out of the habit of the incorrect notion that
    char* is a string type. It is, as I said before, a pointer to a single character. You'll
    avoid lots of stupid allocation mistakes if you use the std::string type for strings. This
    is C++ after all. The whole char* fiasco is a weak carry over from C.

    -Ron
    Ron Natalie, Aug 24, 2004
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lovecreatesbeauty
    Replies:
    1
    Views:
    1,034
    Ian Collins
    May 9, 2006
  2. Brand Bogard

    8 bit character string to 16 bit character string

    Brand Bogard, May 25, 2006, in forum: C Programming
    Replies:
    8
    Views:
    731
    those who know me have no need of my name
    May 28, 2006
  3. george
    Replies:
    0
    Views:
    1,110
    george
    Aug 29, 2008
  4. mohammed_a_o
    Replies:
    0
    Views:
    270
    mohammed_a_o
    Nov 30, 2010
  5. Bart Vandewoestyne
    Replies:
    8
    Views:
    727
    Bart Vandewoestyne
    Sep 25, 2012
Loading...

Share This Page