A Question about new vs malloc and realloc.

From: DrBob (bobrien18_at_yahoo.com)
Date: 11/26/03


Date: 26 Nov 2003 13:41:32 -0800

gcc 3.3 on Mac OS X.

I need to dynamically grow a buffer by concatinating raw binary data
in chunks.

How do I use 'new' to grow the buffer size as its contents grow? I
know this can be done with realloc.. (realloc will give you a new
pointer to a new buffer that has the same contents as the previous
buffer and will free the previous buffer for you).

Perhaps there is a different class I could be using?



Relevant Pages

  • Re: Buffer or Realloc?
    ... better to allocate memory and realloc it for the size of the what is ... between deciding to use a fixed size buffer or allocating memory ... Fixed size arrays are good for things that whose contents *CANNOT* ... becomes extremely small when I realloc it. ...
    (comp.lang.c)
  • Re: Buffer growing strategy?
    ... I'm looking into a buffer ... A very simple solution is to just call realloc for the new size ... The problem is not with the cost of an individual realloc ... If the allocator is a buddy system allocator there only be log n ...
    (comp.lang.c)
  • Re: Bug analysis
    ... char *ReadTextFile ... while (fgets(buffer, sizeof buffer, fp) { ... and in my view it /is/ a bug. ... Well, no, it is better to change the realloc. ...
    (comp.lang.c)
  • Re: openssh vulnerability
    ... > second value to realloc). ... call clean-up functions, and apparently one of those clean-up ... functions may call buffer_free() on that same buffer that's in ... talking about writing nulls... ...
    (Vuln-Dev)
  • Re: Bug analysis
    ... char *ReadTextFile ... while (fgets(buffer, sizeof buffer, fp) { ... and in my view it /is/ a bug. ... Well, no, it is better to change the realloc. ...
    (comp.lang.c)