'Freeing' storage with realloc 
Author Message
 'Freeing' storage with realloc


<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);

Quote:
>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 {*filter*} 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 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://www.*-*-*.com/ ;or<ftp://csd4.csd.uwm.edu/pub/
internetwork-mail-guide>. -- < http://www.*-*-*.com/ ;
fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]



Tue, 04 Aug 1998 03:00:00 GMT  
 'Freeing' storage with realloc

Quote:

>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...

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

Yep.

Quote:
>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.


Tue, 04 Aug 1998 03:00:00 GMT  
 'Freeing' storage with realloc

Quote:
>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.

s/should/may

Dan
--
Dan Pop
CERN, CN Division

Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



Wed, 05 Aug 1998 03:00:00 GMT  
 'Freeing' storage with realloc

Quote:

> Please look at the snipped of code:

> #include <stdio.h>

> 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 */

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

> 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?

Before answering your real question you should be aware of two errors in your
code: 1) you don't check the return value of malloc() and could end up with an ugly
segmentation fault in your strcpy(). 2) Should realloc() fail you will have a memory leak of
the storage pointed to by buf, having trashed that pointer.  So I would rewrite like this:

    char *buf, *tmp;

    buf = malloc(100);
    if (!buf)
        ERROR_HANDLING;
    strcpy(buf, "Klaus Berndl");

    tmp = realloc(buf, strlen(buf) + 1);
    if (!tmp)
        ERROR_HANDLING;
    else
        buf = tmp;

As for your real question about realloc():  it may free the extra bytes, but then again,
maybe NOT .  The first malloc() will return a space of at least 100 bytes.  The realloc(),
only asking for 13 bytes, may do nothing and return buf (after determining that 87 bytes aint
worth the effort) or it might free the extra bytes.  The standard does not specify a memory
management algorithm.  

Don't make any assumptions about the underlying algorithms and know that your new space is
guaranteed to have only 13 bytes.

------------------------------------------------------------------------

Applied Computing Systems Institute of Massachusetts, Inc. (ACSIOM)
Center for Intelligent Information Retrieval
Computer Science Department
University of Massachusetts, Amherst, MA 01003
-----------------------------------------------------------------------------



Wed, 05 Aug 1998 03:00:00 GMT  
 'Freeing' storage with realloc

Quote:

>Please look at the snipped of code:

[ok, snipped :-) ]

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

[and more snipped]

Quote:
>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?

My question is: why do you care?  But you cannot safely access more
than strlen(buf)+1 bytes of buf after the realloc call.  The OS
or (more likely) the C library may or may not re-use that space, and
whether it does may depend on other factors (such as the particular
arrangement of allocated vs. unallocated memory at the time of the
call).

Cheers,
Stan.
--



Sun, 09 Aug 1998 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. 'Freeing' storage with realloc

2. trouble with realloc'ing array of char *'s

3. Can't 'free'

4. free() doesn't free :(

5. how to prevent 'free'-ing memory twice

6. Q:Frequent malloc'ing and free'ing

7. 'free' causes program crash

8. 'free' strategy

9. Crash in 'free' function

10. Free 'C'?

11. Quickie on free()'ing malloc()'d mem

 

 
Powered by phpBB® Forum Software