Need to free memory?

Discussion in 'C++' started by Curious, Aug 1, 2008.

  1. Curious

    Curious Guest

    I have code below:

    char Cities[128], States[12];

    Before I exit the method, do I need to

    delete Cities;
    delete States;

    in order to free the space? Thanks!
     
    Curious, Aug 1, 2008
    #1
    1. Advertising

  2. Curious

    Rolf Magnus Guest

    Curious wrote:

    > I have code below:
    >
    > char Cities[128], States[12];
    >
    > Before I exit the method, do I need to
    >
    > delete Cities;
    > delete States;
    >
    > in order to free the space? Thanks!


    No. You must not delete anything you didn't get from new.
     
    Rolf Magnus, Aug 1, 2008
    #2
    1. Advertising

  3. Curious

    Default User Guest

    Curious wrote:

    > I have code below:
    >
    > char Cities[128], States[12];
    >
    > Before I exit the method, do I need to
    >
    > delete Cities;
    > delete States;
    >
    > in order to free the space? Thanks!


    No. If it wasn't allocated with new, then it isn't deallocated with
    delete. And it would be delete[] anyway.




    Brian
     
    Default User, Aug 1, 2008
    #3
  4. Curious

    Curious Guest

    Thanks Brian for the comment!

    However, I heard that

    char Cities[128];

    is equivalent to

    char *Cities = new char[128];

    And therefore, it should be freed with the following:

    delete Cities[];

    ?
     
    Curious, Aug 1, 2008
    #4
  5. Curious

    Ian Collins Guest

    Curious wrote:
    > Thanks Brian for the comment!
    >
    > However, I heard that
    >
    > char Cities[128];
    >
    > is equivalent to
    >
    > char *Cities = new char[128];
    >

    You heard wrong. The two are completely different.

    It looks like you should get a better book!

    --
    Ian Collins.
     
    Ian Collins, Aug 1, 2008
    #5
  6. Curious

    Default User Guest

    Curious wrote:

    > Thanks Brian for the comment!
    >
    > However, I heard that
    >
    > char Cities[128];
    >
    > is equivalent to
    >
    > char *Cities = new char[128];


    It's not. If a person told you that, never listen to them again. If a
    book told you that, burn it.

    In the first example, Cities[128] is an array of type 128 of char. In
    the second, Cities is a pointer to char. In the first, the memory
    represented by the array can't be resized or relocated. In the second
    it can.

    There are many other differences. You'd be better off using std::vector
    for the most part.




    Brian
     
    Default User, Aug 1, 2008
    #6
  7. Curious

    Curious Guest

    Thanks Brian for the explanation!
     
    Curious, Aug 1, 2008
    #7
  8. Curious

    daniel Guest

    On Aug 1, 11:15 pm, Curious <> wrote:
    > I have code below:
    >
    >                char Cities[128], States[12];
    >
    > Before I exit the method, do I need to
    >
    >                delete Cities;
    >                delete States;
    >
    > in order to free the space? Thanks!


    if you are allocating Cities[128] in a function , then Cities will be
    placed on the stack
    and it will be automatically deleted at function end.
     
    daniel, Aug 4, 2008
    #8
    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. sam
    Replies:
    2
    Views:
    2,276
    Dhruv
    Jun 27, 2003
  2. Replies:
    1
    Views:
    858
    Sebastian Millies
    Nov 2, 2006
  3. Panduranga Chary

    How memory function free() knows how much memory to free.

    Panduranga Chary, Dec 27, 2007, in forum: C Programming
    Replies:
    2
    Views:
    444
    Keith Thompson
    Dec 27, 2007
  4. george
    Replies:
    0
    Views:
    1,204
    george
    Aug 29, 2008
  5. mohammed_a_o
    Replies:
    0
    Views:
    329
    mohammed_a_o
    Nov 30, 2010
Loading...

Share This Page