A Question about new vs malloc and realloc.

Discussion in 'C++' started by DrBob, Nov 26, 2003.

  1. DrBob

    DrBob Guest

    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?
     
    DrBob, Nov 26, 2003
    #1
    1. Advertising

  2. "DrBob" <> wrote...
    > 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).



    Use 'new' to allocate another "buffer", copy the original "buffer"
    there, then add whatever is needed. Then use 'delete' to free the
    original.

    >
    > Perhaps there is a different class I could be using?


    Different class of what?
     
    Victor Bazarov, Nov 26, 2003
    #2
    1. Advertising

  3. DrBob

    Unforgiven Guest

    DrBob wrote:
    > 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).


    realloc is basically a sequence of malloc new buffer, memcpy, free old
    buffer. So you can just as easily do that yourself with new: create new
    buffer, memcpy, delete old buffer.

    Note that this has the danger of becoming a real performancekiller as your
    buffer grows, since more and more data will need to be copied everytime you
    grow. The best way to avoid too much copying if you must do this is probably
    to double your buffer everytime, instead of just adding a single chunk size.

    > Perhaps there is a different class I could be using?


    std::vector<unsigned char> will do all of the above for you with no
    additional effort. It will double the buffer whenever it runs out of space.

    A better approach might be keeping all the separate buffers in a
    std::list<unsigned char*> or std::list<std::vector<unsigned char> >, then
    when you have them all simply create a single buffer that's large enough for
    all buffers and copy all the individual buffers into it. This minimizes
    resizing and copying. It depends on what you're trying to do though.

    --
    Unforgiven

    "You can't rightfully be a scientist if you mind people thinking
    you're a fool."
     
    Unforgiven, Nov 26, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bren
    Replies:
    8
    Views:
    2,069
    Stephen Howe
    Sep 4, 2003
  2. Jun Woong

    Re: Override malloc,calloc,realloc and free?

    Jun Woong, Jun 26, 2003, in forum: C Programming
    Replies:
    0
    Views:
    1,112
    Jun Woong
    Jun 26, 2003
  3. Dan Pop
    Replies:
    0
    Views:
    917
    Dan Pop
    Jun 26, 2003
  4. Douglas A. Gwyn

    Re: Override malloc,calloc,realloc and free?

    Douglas A. Gwyn, Jun 26, 2003, in forum: C Programming
    Replies:
    0
    Views:
    761
    Douglas A. Gwyn
    Jun 26, 2003
  5. Dan Pop
    Replies:
    2
    Views:
    2,086
    Jun Woong
    Jun 27, 2003
Loading...

Share This Page