Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Error When trying to free a pointer

Reply
Thread Tools

Error When trying to free a pointer

 
 
vaysagekv
Guest
Posts: n/a
 
      09-09-2012
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(186
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.

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      09-09-2012
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(186
> 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."
 
Reply With Quote
 
 
 
 
vaysagekv
Guest
Posts: n/a
 
      09-09-2012
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(186
>> 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

 
Reply With Quote
 
vaysagekv
Guest
Posts: n/a
 
      09-09-2012
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(186
>> 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.

 
Reply With Quote
 
Joe Pfeiffer
Guest
Posts: n/a
 
      09-09-2012
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(186
>>> 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?


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      09-09-2012
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(186
>>> 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) kst- <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"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
free free 100 dollors free 4days only FRee REGISTER ONLy h Computer Support 0 04-17-2008 07:43 AM
free free 100 dollors free 4days only FRee REGISTER ONLy h Digital Photography 0 04-17-2008 07:40 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
I got a good news here. Free guest book, free URL direct, free counter, free minipoll and much more!!! Why don't you join? cyx.thunderfoot@gmail.com HTML 1 07-06-2005 12:25 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57