🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: 'Freeing' storage with realloc

#2012
From: Jason Tyler
Date: Fri, 16 Feb 1996 06:11
29 lines
1244 bytes
berndl@sidonius.fmi.uni-passau.de (Klaus Berndl) writes:


>Please look at the snipped of code:
>main () {

>  char* buf = NULL;

>  buf = (char*)malloc(100);
>  strcpy(buf, "Klaus Berndl");
>  printf("\n%s\n", buf);

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

>My questions are now: How much memory is allocated for 'buf' after
>executing line 10?! 

Whatever the second argument to realloc() evaluated to. I couldn't be bothered
counting the letters...

>Does realloc 'freeing' the storage behind byte 13?

Yep.

>Or are still 100 bytes allocated for buf?

No. Just try writing a little program that malloc()s something huge, then
realloc() it down to something tiny, then writes to the end of the original
(huge) buffer. You should get a segfault.
Technical Details
Message-ID: <4g1771$ms3@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: jason@nc.bhpese.oz.au (Jason Tyler)
Newsgroups: comp.lang.c.moderated
Subject: Re: 'Freeing' storage with realloc
Date: 16 Feb 1996 00:11:45 -0600
Organization: BHP IT, Newcastle, Australia
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4g1771$ms3@solutions.solon.com>
References: <4fvhro$e7u@solutions.solon.com>
NNTP-Posting-Host: solutions.solon.com