Skip to main content
gamedev.net
🔒 Locked

Byte string and vector question

Started by Maega Aug 15, 2003 at 10:45 PM 1 replies 250+ views
Original Post
Maega
Maega
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?
Shannon Barber
Shannon Barber
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]
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Maega
Maega
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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.