🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c.moderated
Article #2004

Re: 'Freeing' storage with realloc

#2004
From: Tanmoy Bhattacharya
Date: Fri, 16 Feb 1996 06:06
84 lines
3803 bytes
In article <4fvhro$e7u@solutions.solon.com>
berndl@sidonius.fmi.uni-passau.de (Klaus Berndl) writes: 
<snip>
   #include <stdio.h>

   main () {

Stylistically, one should write int main(void) { or, at the very
least, int main(). What you have written is perfectly correct though. 

     char* buf = NULL;

     buf = (char*)malloc(100);

>From now on, anything can happen. You called maclloc without including
<stdlib.h>, or otherwise including a prototype in scope. All ANSI
conforming compilers would warn you about it, but you used a _cast_:
which tells the compiler `shut up you idiot, I know what I am
doing'. You didn't ... and the blame's on you!

So, strictly speaking, I do not _know_ what your compiler does after
this point. I will answer the rest assuming you have corrected this
error. 

     strcpy(buf, "Klaus Berndl");

Same here. A compiler could have complained, but was not smart enough
to. Why is it so difficult to remember: _every_ place where you use a
library function, ask yourself `Have I included the necessary header?'
Why give the compiler the license to play tick-tack-toe instead of
compiling your program ... it's a tool, use it.

     printf("\n%s\n", buf);

Ahh, at last the header for this one is included. I wonder why.

   /* line 10 */  buf = (char*)realloc(buf, strlen(buf)+1);  /* line 10 */

No prototype for calloc either. Problem hidden by cast.

     if (buf)
       printf("\n%s\n", buf);
     else
       printf("\nshit!\n");

Well, if buf is NULL, you have left yourself no way to recover
... that's why you print something that is presumably illegal on the
net in the US :-) Usually, you set tmp = realloc ..., then test tmp
and set buf=tmp. That way, you can at least free all of buf if tmp
fails, or use buf as it is, etc.

[I have decided to allow allegedly obscene materials if they are part of
 a topical post.  I can't speak for my government, but *I* live in a country
 that put freedom of speech, however offensive that speech may be, in the
 books from very early on. -mod]

   }

   My questions are now: How much memory is allocated for 'buf' after
   executing line 10?! Does realloc 'freeing' the storage behind byte 13?
   Or are still 100 bytes allocated for buf?

Depends on exactly what you mean by freeing it. If your program uses
the part after the 13th character, anything can happen. The C language
does not actually define whether some space goes back to the OS or
not, but it does say that the space after the 13th character is
available for further allocation. As allocation may have arbitrary
overhead, however, there is no guarantee that there be an observable
effect. 

   I`m working with Solaris 2.4 and SUNOS 4.1.3C

If you want an answer specific to your machine (and compiler), ask in
the relevant newsgroup.

Cheers
Tanmoy
--
tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
<http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
Technical Details
Message-ID: <4g16ti$mme@solutions.solon.com>
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
References: <4fvhro$e7u@solutions.solon.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
From: tanmoy@qcd.Lanl.GOV (Tanmoy Bhattacharya)
Newsgroups: comp.lang.c.moderated
Subject: Re: 'Freeing' storage with realloc
Date: 16 Feb 1996 00:06:42 -0600
Organization: Los Alamos National Laboratory
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4g16ti$mme@solutions.solon.com>
References: <4fvhro$e7u@solutions.solon.com>
NNTP-Posting-Host: solutions.solon.com