2

I've got an array of bytes, declared like so:

typedef unsigned char byte;

vector<byte> myBytes = {255, 0 , 76 ...} //individual bytes no larger in value than 255

The problem I have is I need to access the raw data of the vector (without any copying of course), but I need to assign an arbitrary amount of bits to any given pointer to an element.

In other words, I need to assign, say an unsigned int to a certain position in the vector.

So given the example above, I am looking to do something like below:

myBytes[0] = static_cast<unsigned int>(76535); //assign n-bit (here 32-bit) value to any index in the vector

So that the vector data would now look like:

{2, 247, 42, 1} //raw representation of a 32-bit int (76535)

Is this possible? I kind of need to use a vector and am just wondering whether the raw data can be accessed in this way, or does how the vector stores raw data make this impossible or worse - unsafe?

Thanks in advance!

EDIT

I didn't want to add complication, but I'm constructing variously sized integer as follows:

//**N_TYPES
    u16& VMTypes::u8sto16(u8& first, u8& last) {
    return *new u16((first << 8) | last & 0xffff);
}

u8* VMTypes::u16to8s(u16& orig) {
    u8 first = (u8)orig;
    u8 last = (u8)(orig >> 8);
    return new u8[2]{ first, last };
}

What's terrible about this, is I'm not sure of the endianness of the numbers generated. But I know that I am constructing and destructing them the same everywhere (I'm writing a stack machine), so if I'm not mistaken, endianness is not effected with what I'm trying to do.

EDIT 2

I am constructing ints in the following horrible way:

u32 a = 76535;
u16* b = VMTypes::u32to16s(a);
u8 aa[4] = { VMTypes::u16to8s(b[0])[0], VMTypes::u16to8s(b[0])[1], VMTypes::u16to8s(b[1])[0], VMTypes::u16to8s(b[1])[1] };

Could this then work?:

memcpy(&_stack[0], aa, sizeof(u32));
4
  • 1
    Also don't forget about watching for unaligned access. Commented Feb 29, 2016 at 19:49
  • @LogicStuff Yes- but your comment is unhelpful. SuperCookie, new is used for dynamic memory allocation and returns a pointer; better to let the vector manage the memory for you. Commented Feb 29, 2016 at 19:52
  • The important point here is if your system is little endian or big endian. The sequence of the bytes should be reversed, may be, according to your system. Commented Feb 29, 2016 at 19:54
  • @LogicStuff Not helpful. Commented Feb 29, 2016 at 20:15

2 Answers 2

3

Yes, it is possible. You take the starting address by &myVector[n] and memcpy your int to that location. Make sure that you stay in the bounds of your vector.

The other way around works too. Take the location and memcpy out of it to your int.

As suggested: by using memcpy you will copy the byte representation of your integer into the vector. That byte representation or byte order may be different from your expectation. Keywords are big and little endian.

Sign up to request clarification or add additional context in comments.

3 Comments

Any endianess portability concerns?
Thank you for your answer, it seems the most applicable. In light of the two edits I made, do you think endianness would be a concern to me, using this method in conjunction with my integer construction and decoding functions?
That depends on why you need raw access or constructing integers that way. If your data stays on the same machine then there wouldn't be any difference. If it is for serialization the you should choose little or big endian. Implement your serialization function properly so that values will converted when read or written.
0

As knivil says, memcpy will work if you know the endianess of your system. However, if you want to be safe, you can do this with bitwise arithmetic:

unsigned int myInt = 76535;
const int ratio = sizeof(int) / sizeof(byte);
for(int b = 0; b < ratio; b++)
{
    myBytes[b] = byte(myInt >> (8*sizeof(byte)*(ratio - b)));
}

The int can be read out of the vector using a similar pattern, if you want me to show you how let me know.

2 Comments

Shouldn't that 8 be a CHAR_BIT and those magic numbers be sizeof expressions ;)
@JamesRoot Yes. Ill fix it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.