This is the mail archive of the glibc-linux@ricardo.ecn.wfu.edu mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: malloc problem/question in 2.1.3


Kathy Bieltz wrote:

> Mike,
>
> Do you do any XWindows programming???  One of the more
> common and hard to track down problems are SIGSEGV's
> caused by inadvertently freeing memory that was not previously
> allocated.
>
> char* ptr = NULL ;
>
> if (ptr != NULL) {
>    free(ptr) ;
>    ptr = NULL ;
> }
>
> This makes sure you don't inadvertently free memory not previously
> allocated.
>

Did you read that in a book and if yes, then was it on ANSI C and which book was it,
title, author, ed year, and print year?  Where did you learn this, exactly, or not
exactly if you believe it better to not be precise?

> It's quite common to define pointers and then use malloc to
> allocate at runtime only the memory needed to display an image.

It's common among those who do programming involving images, however the same idea
applies to other purposes or applications.  That's what malloc, calloc, etc. are for
and why they were created, to allocate memory at runtime.

> If it's a photo display program, many different sized images may
> be displayed in one program and the same pointer to the image
> data would be free'd and re-allocated for each new image selected.

Could use realloc.  However, without an example of the kind of code you're referring
to, both realloc and redefining ptr to null may be unnecessary, because malloc and
calloc both return null upon failure.

Snipped out the rest.

mike





Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]