Home Ask Login Register

Developers Planet

Your answer is one click away!

vopi181 June 2017

How to copy a vector to a subvector of another vector without segfaulting?

I have some code that looks like this:

for (int i; i > (chip.rom.size() - 1); i++) {
        //int i = 0;
        chip.memory[i + 512] = chip.rom[i];


chip is a struct where memory and rom are both vectors of unsigned chars

rom is a vector of 160 bytes which is the game rom that I'm using to test my emulator.

memory is zeroed to 4096 bytes like so:

chip.memory = std::vector<BYTE>(4096);

Upon debugging, I managed to found out that I was seg faulting after this for statement. I feel like im going crazy! What obvious error am I missing? Thank you guys in advance!


Omnifarious June 2017

You don't initialize i and your comparison is in the wrong direction. Also, if you use < for your comparison, the - 1 is unnecessary if you're just trying to avoid running off the end of chip.rom.

Post Status

Asked in June 2017
Viewed 2,852 times
Voted 13
Answered 0 times


Leave an answer

Quote of the day: live life