linux-gcc.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Lei Yang <leiyang@nec-labs.com>
To: linux-c-programming@vger.kernel.org, linux-gcc@vger.kernel.org
Cc: Lei Yang <leiyang@nec-labs.com>
Subject: Segmentation fault from free()
Date: Tue, 24 Aug 2004 16:34:24 -0400	[thread overview]
Message-ID: <412BA650.6050305@nec-labs.com> (raw)

Hi friends,

I am writing a c code and have been bugged by this segmentation fault 
for a while.

What I did is simply like:

-----------------------------------------------------------------------------------------------------
.........
unsigned long blocksize = 2048;
char *in_buffer
char *out_buffer;
if(( in_buffer= malloc(blocksize)) == NULL)
{
fprintf(stderr, "*** Can't malloc(%ld) forbuffer.\n",blocksize);
return NULL;
}

if(( out_buffer= malloc(2*blocksize)) == NULL)
{
fprintf(stderr, "*** Can't malloc(%ld) forbuffer.\n",blocksize);
free(in_buffer);
return NULL;
}

loop: until all the data are read from file
{
//read a block of data from a file to in_buffer
// do some data processing with in_buffer
//write the result to out_buffer
//memcpy out_buffer to list
}

free(in_buffer);
free(out_buffer);

return list;
......
-----------------------------------------------------------------------------------------------------------------

I've debugged with gdb to see where the segmentation fault happens, it 
is at free(in_buffer).
I've verified that the value for in_buffer after malloc() and before 
free() is the same. Or in other words, in_buffer is a valid pointer 
allocated by malloc.

And the SF only happens when the file is large, although block size 
could be small.
Means that for both small (2KB) and large(5MB) files, block size are the 
same. However, only large files could cause SF.

Could anyone please point me out what could possibly be the reason?
BTW, pls cc me when you reply, since I am not able to receive emails 
from this list. Thanks a lot!

TIA!
Lei

             reply	other threads:[~2004-08-24 20:34 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-08-24 20:34 Lei Yang [this message]
2004-08-24 21:12 ` Segmentation fault from free() Robert Schiele
2004-08-24 21:25   ` Lei Yang
2004-08-24 21:49     ` Robert Schiele
2004-08-25  3:05 ` joy
2004-08-27 20:26 ` Mariano Moreyra

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=412BA650.6050305@nec-labs.com \
    --to=leiyang@nec-labs.com \
    --cc=linux-c-programming@vger.kernel.org \
    --cc=linux-gcc@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).