Re: malloc trouble



I definately chopped this up to shrink it down quite a bit, but yea, my
replies are inline.

Artie Gold wrote:
> > Thank you for pointing out the obvious (faq)--some days I deserve to
> > be slapped :P
> You're obviously new around here. *That* was no slap! ;-)
Hehe, no, but I practically slapped myself over some of the stupid
errors I've done. :grin:


> > messages = malloc(num_messages*sizeof(char *));
> Why bother? Leave this line out.
Hmm...how would I do the realloc later then if the space was never
alloc'd? Or would that be unnecessary as well? :slightly confused:


> > if ( (messages = realloc(messages, (num_messages+1)*sizeof(char *))) == NULL)
> Though it's not applicable here (as receiving a NULL from realloc will
> lead to an immediate exit, as per the code below), you should assign the
> return value of realloc() to a temporary variable, just in case it
> fails. Otherwise (assuming your program can continue) you've created a
> memory leak as you've lost the pointer to the original buffer.
Hmm...I *think* I see what you're saying. By memory leak, I'm infering
that you mean memory that was never free()d for other applications to
use.


> Also, instead of using `sizeof (char *)' use `sizeof *messages'; it's a
> style issue, to be sure (and not immediately important here) but it
> helps in cases where the type you're allocating may change [it's always
> preferable to have to make as few changes as possible when something,
> well, changes.]
Heh, alrighty :) I'll try to keep that in mind next time so you don't
kill me (JPJP)


> > if ( (messages[num_messages] = (char *)malloc((size_t)BUFFLEN)) ==NULL)
> Don't cast the return value of malloc(). It's unnecessary and can hide
> errors.
Hmm...I'm not too sure right now how it'd hide errors, but hey! It'll
make sense probably later on, so lets not worry too much about that.
I'll just take your word for it ;)


> > memset(messages[x], '\0', sizeof(messages[x]));
> Why bother? You're just about to free the space anyway!
Eh, I'm one of those odd people that likes to clean stuff up after
usage. But in retrospect, wasted CPU (however little).


> You're getting there.
Horray :P /Rand


Thanks AG and have a GREAT day :)
-Wes

.



Relevant Pages

  • Re: Why does this segfault?
    ... It's indeed a fatal error to use strcat() on memory you didn't ... initialize with some known string first. ... > and if so used callocinstead of realloc(). ... introduced a memory leak, or that reallocshould have been an ...
    (comp.unix.programmer)
  • Memory Allocation/Accounting bug?
    ... I was doing some leaktests with our applications and couldn't really ... figure out any memory leak. ... PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND ...
    (Linux-Kernel)
  • Re: A problem about win mobile
    ... app) that is causing the OS to forcibly shut down applications? ... The OS doesn't close apps due to low power conditions. ... then results in memory leak in os. ...
    (microsoft.public.windowsce.platbuilder)
  • Re: Tuning: which applications are writing too much
    ... our applications - or some of them - are generating more data than our hardware can stomach. ... a memory leak in the SGA, but an "order by" clause seemed ...
    (comp.databases.oracle.server)
  • Re: Memory leak in Office 2008?
    ... Thanks very much, John, for the heads-up on the Progress Window. ... My memory leak returned this morning. ... Because I had un-installed other recently installed applications, ... I was just about to do an erase-and-clean install of Leopard and re-install ...
    (microsoft.public.mac.office)