Freeing an array of pointer - Unix

This is a discussion on Freeing an array of pointer - Unix ; hi I have an array of pointers defined as int *sampleBuffer[10]; I use malloc to declare space for each index of the array like this: for (i = 0; i doubleBuffer[i] = malloc ( (i+1) * sizeof(int) ); Then I ...

+ Reply to Thread
Results 1 to 6 of 6

Thread: Freeing an array of pointer

  1. Freeing an array of pointer

    hi
    I have an array of pointers defined as

    int *sampleBuffer[10];

    I use malloc to declare space for each index of the array like this:
    for (i = 0; i < 10; i++)
    doubleBuffer[i] = malloc ( (i+1) * sizeof(int) );

    Then I try to free all the malloc spaces like this and I get an
    error.

    for (i = 0; i < 10; i++)
    free(doubleBuffer[i]);

    The error is:
    *** glibc detected *** free(): invalid next size (normal): 0x0878ea88
    ***
    Abort

    Any ideas what I am doing wrong?

    thanks


  2. Re: Freeing an array of pointer

    On 14 Apr 2007 21:29:14 -0700 "Jack" wrote:
    > hi
    > I have an array of pointers defined as
    >
    > int *sampleBuffer[10];
    >
    > I use malloc to declare space for each index of the array like this:
    > for (i = 0; i < 10; i++)
    > doubleBuffer[i] = malloc ( (i+1) * sizeof(int) );
    >
    > Then I try to free all the malloc spaces like this and I get an
    > error.
    >
    > for (i = 0; i < 10; i++)
    > free(doubleBuffer[i]);
    >
    > The error is:
    > *** glibc detected *** free(): invalid next size (normal): 0x0878ea88
    > ***
    > Abort
    >
    > Any ideas what I am doing wrong?


    You should really get an error about doubleBuffer not being defined.
    Also, you are not checking the return value from malloc().

    -frank

  3. Re: Freeing an array of pointer

    On Apr 14, 9:47 pm, Frank Cusack wrote:
    > On 14 Apr 2007 21:29:14 -0700 "Jack" wrote:
    >
    >
    >
    > > hi
    > > I have an array of pointers defined as

    >
    > > int *sampleBuffer[10];

    >
    > > I use malloc to declare space for each index of the array like this:
    > > for (i = 0; i < 10; i++)
    > > doubleBuffer[i] = malloc ( (i+1) * sizeof(int) );

    >
    > > Then I try to free all the malloc spaces like this and I get an
    > > error.

    >
    > > for (i = 0; i < 10; i++)
    > > free(doubleBuffer[i]);

    >
    > > The error is:
    > > *** glibc detected *** free(): invalid next size (normal): 0x0878ea88
    > > ***
    > > Abort

    >
    > > Any ideas what I am doing wrong?

    >
    > You should really get an error about doubleBuffer not being defined.
    > Also, you are not checking the return value from malloc().
    >

    oops, my bad. sampleBuffer is the same as doubleBuffer. No differ in
    this example. Malloc does allocate the memory request because I can
    write to it. I just can't free it after I've used.



  4. Re: Freeing an array of pointer

    "Jack" writes:

    > I have an array of pointers defined as

    ....
    > I use malloc to declare space for each index of the array like this:

    ....
    > Then I try to free all the malloc spaces like this and I get an
    > error.

    ....
    > *** glibc detected *** free(): invalid next size (normal): 0x0878ea88 ***
    > Abort
    >
    > Any ideas what I am doing wrong?


    You are corrupting heap between malloc()s and free()s.

    Since you are (apparently) on Linux/x86, valgrind is your friend --
    it will tell you exactly where your bug is.

    Cheers,
    --
    In order to understand recursion you must first understand recursion.
    Remove /-nsp/ for email.

  5. Re: Freeing an array of pointer

    On 14 Apr 2007 21:54:16 -0700 "Jack" wrote:
    > On Apr 14, 9:47 pm, Frank Cusack wrote:
    >> You should really get an error about doubleBuffer not being defined.
    >> Also, you are not checking the return value from malloc().
    >>

    > oops, my bad. sampleBuffer is the same as doubleBuffer. No differ in
    > this example. Malloc does allocate the memory request because I can
    > write to it. I just can't free it after I've used.


    What I really meant was, you should post exactly the code that is
    failing, in its entirety. If you ran the sample code you posted,
    you'd see that it works (when you correct the typos). That's useless
    if you want help.

    -frank

  6. Re: Freeing an array of pointer

    I can almost guarantee the problem is a buffer overflow in some other
    part of your program, where you write to the array. This could
    corrupt the heap, causing the program to crash when you call free().

    To find out for sure, run your program under valgrind. I'm almost
    sure you'll get an Invalid Write of Size X error somewhere. It will
    tell you the exact line number where it happened.


+ Reply to Thread