linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* 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
* 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  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

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