delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/08/05/14:45:57

From: "Edwin Young" <edwiny AT agentware DOT com>
Newsgroups: comp.os.msdos.djgpp,comp.lang.c
Subject: Re: Realloc()ing data of a node from a linked list
Date: Wed, 5 Aug 1998 11:36:13 -0700
Organization: Slip.Net (http://www.slip.net)
Lines: 27
Message-ID: <6qa904$5jm$1@owl.slip.net>
References: <35c89230 DOT 2078137 AT news3 DOT newscene DOT com>
NNTP-Posting-Host: 207.171.234.216
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Bruno Barberi Gnecco wrote in message <35c89230 DOT 2078137 AT news3 DOT newscene DOT com>...
>      if ( realloc ( pointers.current->data, size ) == NULL ) {


realloc may *move* the data it reallocates to a new location if there isn't enough
space where it is. so you need something like

    unsigned char *newpointer = realloc ( pointers.current->data, size);
    if( ! newpointer)
    {
        alert(...);
    }
    pointers.current->data = newpointer;

In general, you shouldn't just use the simpler

    pointers.current->data = realloc ( pointers.current->data, size);

because if realloc returns NULL then you have effectively leaked the original
block of memory. If you're just going to exit() if you run out of memory then
this may not be a problem.

--
Edwin


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019