release the memory help

Discussion in 'C Programming' started by Rex_chaos, Oct 7, 2003.

  1. Rex_chaos

    Rex_chaos Guest

    I have allocated a array like

    double a[1024];

    We know that a 1-D array just like an 1-D pointer. So I wonder that
    should I release the memory of the array myself like

    free(a);

    Thanks in advance.
    Rex_chaos, Oct 7, 2003
    #1
    1. Advertising

  2. Rex_chaos

    Noah Roberts Guest

    Rex_chaos wrote:
    > I have allocated a array like
    >
    > double a[1024];
    >
    > We know that a 1-D array just like an 1-D pointer. So I wonder that
    > should I release the memory of the array myself like
    >
    > free(a);


    No, a[] is automatic.

    a is not just like a "1-D" pointer either, there are subtle differences;
    I believe this is explained in detail in the C-FAQ.
    >
    > Thanks in advance.
    Noah Roberts, Oct 7, 2003
    #2
    1. Advertising

  3. On 7 Oct 2003 01:59:07 -0700, (Rex_chaos) wrote:

    >double a[1024];
    >
    >We know that a 1-D array just like an 1-D pointer. So I wonder that
    >should I release the memory of the array myself like
    >
    >free(a);


    An array is not the same as a pointer. In many contexts an array may
    decay into a pointer, but they are not just like each other.

    In particular free(a) is undefined. You can only pass free a null
    pointer or a pointer that was generated by an earlier call to malloc,
    calloc or realloc. If any other value is passed to free, or if free
    is called for an argument that has already been deallocated; the
    behavior is undefined.

    Best wishes,

    Bob
    Robert W Hand, Oct 7, 2003
    #3
  4. Rex_chaos

    Mark Gordon Guest

    On 7 Oct 2003 01:59:07 -0700
    (Rex_chaos) wrote:

    > I have allocated a array like
    >
    > double a[1024];
    >
    > We know that a 1-D array just like an 1-D pointer. So I wonder that


    It's similar to, but it is NOT the same and the differences are
    important.

    > should I release the memory of the array myself like
    >
    > free(a);


    You only have to free memory allocated by malloc and friends, or
    functions which use them such as the non-standard strdup function.
    Arrays follow the same rules as any other variable when it comes to
    storage duration so you DON'T free them any more than you free an
    integer variable.
    --
    Mark Gordon
    Paid to be a Geek & a Senior Software Developer
    Although my email address says spamtrap, it is real and I read it.
    Mark Gordon, Oct 7, 2003
    #4
  5. Rex_chaos

    Richard Bos Guest

    (Rex_chaos) wrote:

    > I have allocated a array like
    >
    > double a[1024];
    >
    > We know that a 1-D array just like an 1-D pointer.


    You don't know that. You may think that, but you're mistaken. (You're
    not alone in this, though; it's a very widely propagated myth.) An array
    is really nothing like a pointer, it just mimics one in some, often-
    encountered, circumstances. Behind the scenes, they're really quite
    dissimilar.
    For example, here you've asked for memory for 1024 doubles. You're
    either getting it or not; you haven't got any error recovery. You cannot
    make this memory "point to" another address. The thing is where it is.

    Had you done this:

    double a*;

    a=malloc(1024 * sizeof *a);

    you'd have asked for two bits of memory: one for the pointer itself, and
    one for the 1024 doubles. The first bit is much like the array: it is a
    single, unmovable object. The second bit, though, is more flexible. If
    there isn't enough memory for 1024 doubles, you can recover from this,
    because a is now a null pointer and you can check for that. If you want
    more, you can realloc(). If you want a to point at another block of
    doubles, you can point it there. It's much more flexible; but that
    flexibility comes at the price of having to control it.

    > So I wonder that should I release the memory of the array myself like
    >
    > free(a);


    Certainly not. You only do that with pointers you got from any of the
    *alloc() functions.

    Richard
    Richard Bos, Oct 7, 2003
    #5
  6. Rex_chaos

    Dan Pop Guest

    In <> (Rex_chaos) writes:

    >I have allocated a array like
    >
    >double a[1024];
    >
    >We know that a 1-D array just like an 1-D pointer.


    After reading the FAQ, we no longer "know" that! And we're supposed to
    read the FAQ *before* posting!

    >So I wonder that
    >should I release the memory of the array myself like
    >
    >free(a);


    NEVER pass to free() a pointer value that was not obtained from one of
    the *alloc functions.

    There are three kinds of memory allocation in a C program:

    Static allocation: the memory is allocated for the entire duration of the
    program execution and there is nothing you can do about it.

    Automatic allocation: the memory is allocated for the entire duration of
    the block containing the object definition and automatically deallocated
    when the program execution quits the block. Again, no programmer
    intervention is possible.

    Dynamic allocation: the memory is allocated as a result of on an *alloc
    function call and stays allocated until the program calls free with the
    pointer value returned by the corresponding *alloc function call (or
    until it is reallocated with a realloc function call) or until program
    termination.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Oct 7, 2003
    #6
  7. Rex_chaos

    Alex Guest

    Noah Roberts <> wrote:
    > Rex_chaos wrote:
    >> I have allocated a array like
    >>
    >> double a[1024];
    >>
    >> We know that a 1-D array just like an 1-D pointer. So I wonder that
    >> should I release the memory of the array myself like
    >>
    >> free(a);


    > No, a[] is automatic.


    .... or static.

    > a is not just like a "1-D" pointer either, there are subtle differences;
    > I believe this is explained in detail in the C-FAQ.


    Not subtle at all. They are equivalent only in the formal function
    parameter context.


    Alex
    Alex, Oct 7, 2003
    #7
    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. Mahesh Prasad
    Replies:
    1
    Views:
    697
    Tom Wells
    Feb 22, 2004
  2. John Goche
    Replies:
    1
    Views:
    7,450
    John Goche
    Dec 17, 2005
  3. Benjamin Peterson

    [RELEASE] Python 2.7 release candidate 1 released

    Benjamin Peterson, Jun 6, 2010, in forum: Python
    Replies:
    3
    Views:
    353
    Lie Ryan
    Jun 6, 2010
  4. Benjamin Peterson

    [RELEASE] 3.1.4 release candidate 1

    Benjamin Peterson, May 29, 2011, in forum: Python
    Replies:
    2
    Views:
    186
    Alain Ketterlin
    May 30, 2011
  5. Benjamin Peterson

    [RELEASE] Python 2.7.2 release candidate 1

    Benjamin Peterson, May 29, 2011, in forum: Python
    Replies:
    0
    Views:
    344
    Benjamin Peterson
    May 29, 2011
Loading...

Share This Page