public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* malloc and free
@ 2004-12-24 18:40 Ankit Jain
  2004-12-29  9:54 ` Ken Foskey
  0 siblings, 1 reply; 4+ messages in thread
From: Ankit Jain @ 2004-12-24 18:40 UTC (permalink / raw)
  To: gcc

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] 4+ messages in thread

* Re: malloc and free
  2004-12-24 18:40 malloc and free Ankit Jain
@ 2004-12-29  9:54 ` Ken Foskey
  0 siblings, 0 replies; 4+ messages in thread
From: Ken Foskey @ 2004-12-29  9:54 UTC (permalink / raw)
  To: gcc help

On Fri, 2004-12-24 at 18:40 +0000, Ankit Jain 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?

If you are using Linux X86 try the code under valgrind.

You could also compile your code with -g and run it under gdb.

Otherwise your question is so vague it is impossible to work out how to
help you.

-- 
Ken Foskey
OpenOffice.org developer


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: malloc and free
  2004-12-27 17:38 lrtaylor
@ 2004-12-27 17:45 ` Sriharsha
  0 siblings, 0 replies; 4+ messages in thread
From: Sriharsha @ 2004-12-27 17:45 UTC (permalink / raw)
  To: gcc-help

What Irtaylor said is true:
Consider code such as this....

...
...
while(some_condition)
{
    if(some_other_condition)
    {
        ...
        some_var = malloc(some_amount);
        ...
    }
    ...
    free(some_var);
}
...
...

Here, may be for the first time "some_other_condition" may be true, but 
subsequently, it may be false due to which the free (or any code that 
refers to some_var) fails.


lrtaylor@micron.com wrote:

>It's probably a bug in your routine.  However, without seeing your code,
>it's hard to give any good answers.  Step through it with a debugger and
>make sure that you're not trying to free the same memory more than once,
>or access memory that hasn't been allocated yet.
>
>Good luck,
>Lyle 
>
>-----Original Message-----
>From: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org] On
>Behalf Of Ankit Jain
>Sent: Friday, December 24, 2004 11:41 AM
>To: gcc
>Subject: malloc and free
>
>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
>
>
>
>
>  
>

-- 
 *****************************
 * Sriharsha Vedurmudi			
 * Software Engineer		
 * 
 * Redpine Signals Inc.	
 * Gate #395, Plot 87,88			
 * Sagar Society, Road #2, 
 * Banjara Hills,		
 * Hyderabad - 500 034			
 * www.redpinesignals.com	
 *							
 * +91-40-23559911  (Office)
 * +91-98851-37338  (Mobile)
 *****************************

^ permalink raw reply	[flat|nested] 4+ messages in thread

* RE: malloc and free
@ 2004-12-27 17:38 lrtaylor
  2004-12-27 17:45 ` Sriharsha
  0 siblings, 1 reply; 4+ messages in thread
From: lrtaylor @ 2004-12-27 17:38 UTC (permalink / raw)
  To: ankitjain1580, gcc-help

It's probably a bug in your routine.  However, without seeing your code,
it's hard to give any good answers.  Step through it with a debugger and
make sure that you're not trying to free the same memory more than once,
or access memory that hasn't been allocated yet.

Good luck,
Lyle 

-----Original Message-----
From: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org] On
Behalf Of Ankit Jain
Sent: Friday, December 24, 2004 11:41 AM
To: gcc
Subject: malloc and free

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] 4+ messages in thread

end of thread, other threads:[~2004-12-29  9:54 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-12-24 18:40 malloc and free Ankit Jain
2004-12-29  9:54 ` Ken Foskey
2004-12-27 17:38 lrtaylor
2004-12-27 17:45 ` Sriharsha

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 read-only IMAP folder(s) and NNTP newsgroup(s).