• Advertisement

Archived

This topic is now archived and is closed to further replies.

Byte string and vector question

This topic is 5270 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello. I have a semi-dumb question I think. Lets say I have a variable number of bytes in a byte string. However, I want to be able to place those bytes into a byte array that I can push onto a vector of unsigned char *. So my question is.. how would I best break up the byte string so I can put it into an array, and is vector the best choice to use over say.. list?

Share this post


Link to post
Share on other sites
Advertisement
Is it really a character string, or is it a byte sequence? If it really is a string, use std::string. Typedef it and always use that typedef; later if std::string gives you memory problems, you can try either a customer allocator or even replace it with a shortstring implementation (that's big enough for your largest string).

If it's a byte sequence, you probably want to use some wrapper - something that provides a stream interface (I wrote a circular buffer template to do stuff like this).

Whether list or vector is better depends on the situation. When compiling with MSVC, vector is almost always better (even in cases theory tells us list is faster). This is probably due in large part to the frequent memory (de)allocation using a list can cause and how slow the CRT memory allocator is. To avoid that, I have my own wrapper object that maintains two list; a good list and a 'dead' list. You can use splice to move an element between the two list very quickly (O(1) and it's just a couple of pointer assignments, 4 I think). Then you can batch allocate list nodes and put the extras on the dead list (then allocation is "free" next time).

[edited by - Magmai Kai Holmlor on August 15, 2003 11:59:37 PM]

Share this post


Link to post
Share on other sites
Well the byte string is in a file read with C++ file i/o, so I guess you could consider it a string.

I''ll take your suggestions and see what I come up with

Share this post


Link to post
Share on other sites

  • Advertisement