Error When trying to free a pointer

V

vaysagekv

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.
 
E

Eric Sosman

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.
 
V

vaysagekv

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
 
V

vaysagekv

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.:)
 
J

Joe Pfeiffer

vaysagekv said:
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?
 
K

Keith Thompson

vaysagekv said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top