Error When trying to free a pointer

Discussion in 'C Programming' started by vaysagekv, Sep 9, 2012.

  1. vaysagekv

    vaysagekv Guest

    I have a function like below.The program has compiled without any errors
    ..But when I run it I am getting Error like this --->fibBigNumbers(1868)
    malloc: *** error for object 0x100000ee2: pointer being freed was not
    allocated
    *** set a breakpoint in malloc_error_break to debug
    021Abort trap.

    Program:
    void fibonacci(void)
    {
    static char* i = "1";
    static char* j = "1";

    char* fib = addBigNumbers(i,j);
    printf("\n%s",fib);

    char* temp = i;
    i=j;
    j=fib;
    if (!strcmp(temp,"1"))
    {
    free(temp);
    temp = NULL;
    }
    }

    Please Help.
    Thanks in Advance.
     
    vaysagekv, Sep 9, 2012
    #1
    1. Advertising

  2. vaysagekv

    Eric Sosman Guest

    On 9/9/2012 9:14 AM, vaysagekv wrote:
    > I have a function like below.The program has compiled without any errors
    > .But when I run it I am getting Error like this --->fibBigNumbers(1868)
    > malloc: *** error for object 0x100000ee2: pointer being freed was not
    > allocated
    > *** set a breakpoint in malloc_error_break to debug
    > 021Abort trap.
    >
    > Program:
    > void fibonacci(void)
    > {
    > static char* i = "1";
    > static char* j = "1";
    >
    > char* fib = addBigNumbers(i,j);
    > printf("\n%s",fib);
    >
    > char* temp = i;
    > i=j;
    > j=fib;
    > if (!strcmp(temp,"1"))
    > {
    > free(temp);
    > temp = NULL;
    > }
    > }


    You must not free() memory you did not obtain from malloc()
    (or calloc() or realloc()). When your code executes free(temp),
    `temp' points to the "1" that initialized `i'. The memory
    holding that string was not obtained from malloc(), so you must
    not try to free() it.

    Aside: It is possible that the !strcmp(temp,"1") test is
    confusing you. You may have intended to call free() only when
    `temp' points to something other than "1", but the actual code
    does exactly the opposite: If `temp' points to something equal
    to "1", strcmp(temp,"1") returns zero to say that the strings
    are equal. Then the `!' converts that zero to one, which is
    "true" for the `if' test. A hasty reading of `if(!strcmp(...))'
    seems to say "Do something if the strings are not equal," but
    in fact it says "Do something if the strings *are* equal." For
    this reason (among others), I suggest you stop using the `!strcmp'
    form and write `if(strcmp(...) == 0)' or `if(strcmp(...) != 0)'
    to make your intent clearer.

    --
    Eric Sosman
    d
    "The speed at which the system fails is usually not important."
     
    Eric Sosman, Sep 9, 2012
    #2
    1. Advertising

  3. vaysagekv

    vaysagekv Guest

    On 09/09/12 7:02 PM, pete wrote:
    > vaysagekv wrote:
    >>
    >> I have a function like below.The program has compiled without any errors
    >> .But when I run it I am getting Error like this --->fibBigNumbers(1868)
    >> malloc: *** error for object 0x100000ee2: pointer being freed was not
    >> allocated
    >> *** set a breakpoint in malloc_error_break to debug
    >> 021Abort trap.

    >
    >> static char* i = "1";
    >> char* temp = i;
    >> free(temp);

    >
    > The argument in a call to free(),
    > should be a value which has previously been returned
    > by either malloc or calloc or realloc.
    > free() can also take a null pointer as an argument.
    >

    Hi,
    Thanks for the reply .Here the argument I have given to free is
    allocated by malloc in addBigNumbers function as below.
    char* addBigNumbers(char* first,char* second)
    {
    short lenOfFirst = strlen(first);
    short lenOfSecond = strlen(second);
    short len = lenOfFirst >= lenOfSecond ? lenOfFirst : lenOfSecond;


    char* result= malloc(len + 2);
    short resultLen = len + 1;
    if (result == NULL) {
    printf("Error");
    }
    result[resultLen] = '\0';

    .....
    .....
    .....

    return result;

    }
    Thanks
    vaysage
     
    vaysagekv, Sep 9, 2012
    #3
  4. vaysagekv

    vaysagekv Guest

    On 09/09/12 7:09 PM, Eric Sosman wrote:
    > On 9/9/2012 9:14 AM, vaysagekv wrote:
    >> I have a function like below.The program has compiled without any errors
    >> .But when I run it I am getting Error like this --->fibBigNumbers(1868)
    >> malloc: *** error for object 0x100000ee2: pointer being freed was not
    >> allocated
    >> *** set a breakpoint in malloc_error_break to debug
    >> 021Abort trap.
    >>
    >> Program:
    >> void fibonacci(void)
    >> {
    >> static char* i = "1";
    >> static char* j = "1";
    >>
    >> char* fib = addBigNumbers(i,j);
    >> printf("\n%s",fib);
    >>
    >> char* temp = i;
    >> i=j;
    >> j=fib;
    >> if (!strcmp(temp,"1"))
    >> {
    >> free(temp);
    >> temp = NULL;
    >> }
    >> }

    >
    > You must not free() memory you did not obtain from malloc()
    > (or calloc() or realloc()). When your code executes free(temp),
    > `temp' points to the "1" that initialized `i'. The memory
    > holding that string was not obtained from malloc(), so you must
    > not try to free() it.
    >
    > Aside: It is possible that the !strcmp(temp,"1") test is
    > confusing you. You may have intended to call free() only when
    > `temp' points to something other than "1", but the actual code
    > does exactly the opposite: If `temp' points to something equal
    > to "1", strcmp(temp,"1") returns zero to say that the strings
    > are equal. Then the `!' converts that zero to one, which is
    > "true" for the `if' test. A hasty reading of `if(!strcmp(...))'
    > seems to say "Do something if the strings are not equal," but
    > in fact it says "Do something if the strings *are* equal." For
    > this reason (among others), I suggest you stop using the `!strcmp'
    > form and write `if(strcmp(...) == 0)' or `if(strcmp(...) != 0)'
    > to make your intent clearer.
    >

    Thanks a lot. That strcmp was the error.Thanks for pointing out .Thanks
    again.:)
     
    vaysagekv, Sep 9, 2012
    #4
  5. vaysagekv

    Joe Pfeiffer Guest

    vaysagekv <> writes:

    > On 09/09/12 7:02 PM, pete wrote:
    >> vaysagekv wrote:
    >>>
    >>> I have a function like below.The program has compiled without any errors
    >>> .But when I run it I am getting Error like this --->fibBigNumbers(1868)
    >>> malloc: *** error for object 0x100000ee2: pointer being freed was not
    >>> allocated
    >>> *** set a breakpoint in malloc_error_break to debug
    >>> 021Abort trap.

    >>
    >>> static char* i = "1";
    >>> char* temp = i;
    >>> free(temp);

    >>
    >> The argument in a call to free(),
    >> should be a value which has previously been returned
    >> by either malloc or calloc or realloc.
    >> free() can also take a null pointer as an argument.
    >>

    > Hi,
    > Thanks for the reply .Here the argument I have given to free is
    > allocated by malloc in addBigNumbers function as below.
    > char* addBigNumbers(char* first,char* second)
    > {
    > short lenOfFirst = strlen(first);
    > short lenOfSecond = strlen(second);
    > short len = lenOfFirst >= lenOfSecond ? lenOfFirst : lenOfSecond;
    >
    >
    > char* result= malloc(len + 2);
    > short resultLen = len + 1;
    > if (result == NULL) {
    > printf("Error");
    > }
    > result[resultLen] = '\0';
    >
    > .....
    > .....
    > .....
    >
    > return result;
    >
    > }
    > Thanks
    > vaysage


    To get a useful reply, you need to tell us exactly what the code really
    is. In your original post, the relevant lines were:

    static char* i = "1";

    char* temp = i;

    free(temp);

    so the only free() in that code was a variable that wasn't allocated
    with malloc(). Now you're telling us that that wasn't the code at all,
    that your variable was really malloc()ed inside your addBigNumbers()
    function.

    So -- what does the code really look like?
     
    Joe Pfeiffer, Sep 9, 2012
    #5
  6. vaysagekv <> writes:
    > On 09/09/12 7:02 PM, pete wrote:
    >> vaysagekv wrote:
    >>>
    >>> I have a function like below.The program has compiled without any errors
    >>> .But when I run it I am getting Error like this --->fibBigNumbers(1868)
    >>> malloc: *** error for object 0x100000ee2: pointer being freed was not
    >>> allocated
    >>> *** set a breakpoint in malloc_error_break to debug
    >>> 021Abort trap.

    >>
    >>> static char* i = "1";
    >>> char* temp = i;
    >>> free(temp);

    >>
    >> The argument in a call to free(),
    >> should be a value which has previously been returned
    >> by either malloc or calloc or realloc.
    >> free() can also take a null pointer as an argument.
    >>

    > Thanks for the reply. Here the argument I have given to free is
    > allocated by malloc in addBigNumbers function as below.
    >

    [snip]

    For purposes of your question, it doesn't matter what addBigNumbers()
    does. The argument you pass to free() is `temp`, which is initialized
    to the value of `i`, which points to the first character of the string
    literal "1".

    Passing `i` as an argument to addBigNumbers() cannot change the value of
    `i`.

    In another followup you said something about the call to strcmp() being
    the problem, but I don't think that would affect anything.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Sep 9, 2012
    #6
    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. Jeff Rodriguez
    Replies:
    5
    Views:
    406
    CBFalconer
    Nov 15, 2003
  2. Replies:
    10
    Views:
    744
    Chris Torek
    Feb 4, 2005
  3. jimjim
    Replies:
    16
    Views:
    876
    Jordan Abel
    Mar 28, 2006
  4. george
    Replies:
    0
    Views:
    1,202
    george
    Aug 29, 2008
  5. mohammed_a_o
    Replies:
    0
    Views:
    326
    mohammed_a_o
    Nov 30, 2010
Loading...

Share This Page