By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,798 Members | 1,766 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,798 IT Pros & Developers. It's quick & easy.

A Question about new vs malloc and realloc.

P: n/a
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?
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"DrBob" <bo*******@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?
Jul 22 '05 #2

P: n/a
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."
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.