A Question about new vs malloc and realloc.



 DEVELOP > c-Plus-Plus > A Question about new vs malloc and realloc.

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "DrBob"
Date: 26 Nov 2003 03:41:32 PM
Object: A Question about new vs malloc and realloc.
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?
.

User: "Victor Bazarov"

Title: Re: A Question about new vs malloc and realloc. 26 Nov 2003 04:11:44 PM
"DrBob" <bobrien18@yahoo.com> 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?
.

User: "Unforgiven"

Title: Re: A Question about new vs malloc and realloc. 26 Nov 2003 04:17:06 PM
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."
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER