Re: Segmentation fault at malloc.c:4232



On Sat, 1 Mar 2008 06:10:55 +0800, hanpingtian@xxxxxxxxx wrote:

I just found out when I comment out the line 27, which calls free() to
free the line got from
getline(), the program doesn't segmentation fault.

But the manual of getline() tells us to free the "*lineptr" when it is
NULL before call getline(). It's the situation in my program.

With this src.txt:
(x = 1)
1
it reports "double free or corruption" when doesn't comment line 27:

*** glibc detected *** ./compiler: double free or corruption (!prev): 0x08c0d2d8

Your theory is false, your code is broken. You really need to free()
the line buffer when it is allocated by getline(). But you call getline()
in a while-loop without resetting the line pointer to NULL. Here's the
fix:

--- compiler.c~ 2008-02-29 07:06:46.000000000 +0100
+++ compiler.c 2008-02-29 23:55:41.000000000 +0100
@@ -25,6 +25,7 @@
}

free(line);
+ line = NULL;
STACKfree();
}


--
fedora-list mailing list
fedora-list@xxxxxxxxxx
To unsubscribe: https://www.redhat.com/mailman/listinfo/fedora-list



Relevant Pages

  • Re: Segmentation fault on end of loop... (getline)
    ... i always get a "Segmentation fault". ... > using GNU C: ... getline() is not a standard C function. ... pointer to an uninitialized pointer object (the value of line is ...
    (comp.lang.c)
  • Re: Segmentation fault at malloc.c:4232
    ... the program doesn't segmentation fault. ... But the manual of getline() tells us to free the "*lineptr" when it is ... it reports "double free or corruption" when doesn't comment line 27: ...
    (Fedora)
  • Re: Proposal for the C library in the new standard
    ... those two pointer arguments will be addresses of objects and the chances of "lineptr" or "n" being null pointers will be negligible: ... getline(&line, &len, fp) ... it allows *lineptr to be a null pointer or *n to be zero. ...
    (comp.std.c)
  • Re: print one occurance for duplicate entries
    ... This will fix the syntax errors: ... but almost all solutions that use getline are bad awk style at best or, ... script above work as it's starting way off on the wrong track. ...
    (comp.unix.shell)
  • Re: [std::getline] Avoid having to hit <Enter> twice, How to?
    ... Known bug in VC6. ... There is also fix to it, ... answers with searchwords like getline, enter and twice, you should find ...
    (alt.comp.lang.learn.c-cpp)