Re: Segmentation fault at malloc.c:4232

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, 29 Feb 2008 18:07:22 -0500, Jakub Jelinek wrote:

> On Fri, Feb 29, 2008 at 11:57:53PM +0100, Michael Schwendt wrote:
> > 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.
> 
> True.
> 
> > Here's the fix:
> 
> But this is not how getline is meant to be used.  This way each getline call
> will allocate a new buffer.  getline is designed so that you can use it in a
> loop with just one allocated buffer, which is realloced by getline when
> needed.  So the right fix would be actually to move the free(line);
> statement after the loop.  Of course if you e.g. want to preserve the buffer
> with the line data, stick pointer to it into some data structure, then you'd
> just do that and clear the pointer so that next getline iteration will
> allocate a fresh buffer.

Yeah, but that explanation and optimisation goes beyond pointing out what
causes the segfault in the implicit malloc. ;) Basically, everything about
how getline can be used is explained in the man page already anyway, i.e.
you either start with a NULL lineptr or rely on the automatic realloc. Here
there's even example code in the man page.

-- 
fedora-list mailing list
fedora-list@xxxxxxxxxx
To unsubscribe: https://www.redhat.com/mailman/listinfo/fedora-list
[Index of Archives]     [Older Fedora Users]     [Fedora Announce]     [Fedora Package Announce]     [EPEL Announce]     [Fedora Magazine]     [Fedora News]     [Fedora Summer Coding]     [Fedora Laptop]     [Fedora Cloud]     [Fedora Advisory Board]     [Fedora Education]     [Fedora Security]     [Fedora Scitech]     [Fedora Robotics]     [Fedora Maintainers]     [Fedora Infrastructure]     [Fedora Websites]     [Anaconda Devel]     [Fedora Devel Java]     [Fedora Legacy]     [Fedora Desktop]     [Fedora Fonts]     [ATA RAID]     [Fedora Marketing]     [Fedora Management Tools]     [Fedora Mentors]     [SSH]     [Fedora Package Review]     [Fedora R Devel]     [Fedora PHP Devel]     [Kickstart]     [Fedora Music]     [Fedora Packaging]     [Centos]     [Fedora SELinux]     [Fedora Legal]     [Fedora Kernel]     [Fedora OCaml]     [Coolkey]     [Virtualization Tools]     [ET Management Tools]     [Yum Users]     [Tux]     [Yosemite News]     [Gnome Users]     [KDE Users]     [Fedora Art]     [Fedora Docs]     [Asterisk PBX]     [Fedora Sparc]     [Fedora Universal Network Connector]     [Libvirt Users]     [Fedora ARM]

  Powered by Linux