* RE: malloc and free
@ 2002-05-20 9:55 Alvarez Alberto-AALVARB1
0 siblings, 0 replies; 6+ messages in thread
From: Alvarez Alberto-AALVARB1 @ 2002-05-20 9:55 UTC (permalink / raw)
To: linux-c-programming
Hi,
Why do you 'malloc' for token? strtok returns a pointer to an already allocated string (or a null pointer), but doesn't do a copy. So, when assigning strtok to 'token', you loose the value you had allocated.
So, the free()'ing doesn't free your former allocation, but the piece of string returned by strtok.
Regards,
Alberto.
> -----Original Message-----
> From: Sameer Maggon [mailto:smaggon@dypatil.edu]
> Sent: lunes, 20 de mayo de 2002 13:27
> To: linux-c-programming@vger.kernel.org
> Subject: malloc and free
>
>
> Hi,
> In one function i have a definiton like
>
> char *token;
> token = (char *)malloc(256 * sizeof(char));
>
> ...
> ...
> token = strtok(...,"\0");
>
> ...
> free(token);
>
>
> Now the problem is that sometimes this runs
> fine and sometimes it crashes on free()..
> Why so.
> If I remove the free(..), then the program
> works perfectly fine
>
> How do i correct the problem.
>
> Regards
> Sameer
>
>
>
>
>
> --------------
> http://www.dypatil.edu For Better tomorrow
> -
> To unsubscribe from this list: send the line "unsubscribe
> linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: malloc and free
2002-05-20 11:26 Sameer Maggon
@ 2002-05-20 10:06 ` wwp
2002-05-20 18:16 ` Andrew Edmondson
1 sibling, 0 replies; 6+ messages in thread
From: wwp @ 2002-05-20 10:06 UTC (permalink / raw)
To: Sameer Maggon; +Cc: linux-c-programming
Hi Sameer,
On Mon, 20 May 2002 16:56:30 +0530 (IST) Sameer Maggon <smaggon@dypatil.edu> wrote:
[snip]
> char *token;
> token = (char *)malloc(256 * sizeof(char));
>
> ...
> ...
> token = strtok(...,"\0");
[snip]
The problem is there, you change "token"'s value, so you
lost the value formerly returned by malloc (this one has
to be free()'ed) because strtok returns a pointer to the next
token IIRC. Then, the "token" you attempt to free is not
the good one.
For instance, if I didn't miss anything:
char *token, *buffer;
buffer = (char *)malloc(256 * sizeof(char));
token = buffer;
token = strtok(...,"\0");
..
free(buffer);
Regards,
--
wwp
^ permalink raw reply [flat|nested] 6+ messages in thread
* malloc and free
@ 2002-05-20 11:26 Sameer Maggon
2002-05-20 10:06 ` wwp
2002-05-20 18:16 ` Andrew Edmondson
0 siblings, 2 replies; 6+ messages in thread
From: Sameer Maggon @ 2002-05-20 11:26 UTC (permalink / raw)
To: linux-c-programming
Hi,
In one function i have a definiton like
char *token;
token = (char *)malloc(256 * sizeof(char));
...
...
token = strtok(...,"\0");
...
free(token);
Now the problem is that sometimes this runs
fine and sometimes it crashes on free()..
Why so.
If I remove the free(..), then the program
works perfectly fine
How do i correct the problem.
Regards
Sameer
--------------
http://www.dypatil.edu For Better tomorrow
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: malloc and free
2002-05-20 11:26 Sameer Maggon
2002-05-20 10:06 ` wwp
@ 2002-05-20 18:16 ` Andrew Edmondson
1 sibling, 0 replies; 6+ messages in thread
From: Andrew Edmondson @ 2002-05-20 18:16 UTC (permalink / raw)
To: smaggon; +Cc: linux-c-programming
On Mon, 20 May 2002, Sameer Maggon wrote:
> Hi,
> In one function i have a definiton like
>
> char *token;
> token = (char *)malloc(256 * sizeof(char));
No real need to cast here and in fact it could mask a missing
#include <stdlib.h>.
man malloc
---< snip
RETURN VALUES
For calloc() and malloc(), the value returned is a pointer
to the allocated memory, which is suitably aligned for any
kind of variable, or NULL if the request fails.
---> snip
Also, char is always size 1 so there is no need for the sizeof(). If you did
want to use sizeof() use it with the object not the type so that if you want
to change the type of the object you don't need to also change the malloc:
token = malloc(256 * sizeof(*token));
Also, I assume you are checking the return value of the malloc is not NULL at
some point.
if ( ( token = malloc(256) ) == NULL )
{
puts("Malloc: Memory allocation failed.");
exit(1);
}
>
> ...
> ...
> token = strtok(...,"\0");
Here's the problem, you are assigning another value to token and have lost
track of your malloc'd memory.
>
> ...
> free(token);
Free may now be freeing the wrong memory.
--
Andrew Edmondson
Test Development Engineer
<-------------------------->
The discerning heart seeks knowledge,
but the mouth of a fool feeds on folly.
^ permalink raw reply [flat|nested] 6+ messages in thread
* malloc and free
@ 2004-12-24 18:39 Ankit Jain
2004-12-24 18:52 ` Eric Bambach
0 siblings, 1 reply; 6+ messages in thread
From: Ankit Jain @ 2004-12-24 18:39 UTC (permalink / raw)
To: linux prg
hi
routine xyz uses malloc and free functions. it gives
accurate and correct result if called once.
but if the function is called in a loop N number of
times then probably it gives segmentation fault.
what is the reason? can any body guess or test code
is needed?
thanks
ankit jain
________________________________________________________________________
Yahoo! Messenger - Communicate instantly..."Ping"
your friends today! Download Messenger Now
http://uk.messenger.yahoo.com/download/index.html
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: malloc and free
2004-12-24 18:39 malloc and free Ankit Jain
@ 2004-12-24 18:52 ` Eric Bambach
0 siblings, 0 replies; 6+ messages in thread
From: Eric Bambach @ 2004-12-24 18:52 UTC (permalink / raw)
To: Ankit Jain; +Cc: linux-c-programming
On Friday 24 December 2004 12:39 pm, you wrote:
> hi
>
> routine xyz uses malloc and free functions. it gives
> accurate and correct result if called once.
>
> but if the function is called in a loop N number of
> times then probably it gives segmentation fault.
>
> what is the reason? can any body guess or test code
> is needed?
>
> thanks
>
> ankit jain
A common cause of failure in the malloc/free routines is memory corruption.
The trick about memory corruption and buffer overflows is that they work
sometimes/most of the time. Perhaps you are doing out-of-bounds reading or
(gak!) writing with the memory. Try running it under valgrind to see if this
is the case.
Also, although routine xyz may not corrupt the memory perhaps something else
in your program is? Writing in strange places confuses libc and can result in
a segfault in places that can be quite far away from the original spot of
corruption.
----------------------------------------
--EB
> All is fine except that I can reliably "oops" it simply by trying to read
> from /proc/apm (e.g. cat /proc/apm).
> oops output and ksymoops-2.3.4 output is attached.
> Is there anything else I can contribute?
The latitude and longtitude of the bios writers current position, and
a ballistic missile.
--Alan Cox LKML-December 08,2000
----------------------------------------
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2004-12-24 18:52 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-12-24 18:39 malloc and free Ankit Jain
2004-12-24 18:52 ` Eric Bambach
-- strict thread matches above, loose matches on Subject: below --
2002-05-20 11:26 Sameer Maggon
2002-05-20 10:06 ` wwp
2002-05-20 18:16 ` Andrew Edmondson
2002-05-20 9:55 Alvarez Alberto-AALVARB1
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).