vendredi 22 septembre 2017

Getting a sigseg fault when trying to modify an incrementing index in two separate arrays

So I'm trying to separate an array of char into blocks of 4 characters, keep track of whether the current block is even or odd, and rearrange them in increasing odd order and then increasing even order. I have an int variable to keep track of which index I'm at in the array and a for loop to determine if the current block is odd and if it is, put those characters into arrayOne, and if they're even put them into arrayTwo. At the end I would put arrayOne and arrayTwo together. However I think my incrementing idx variable in the loop checking if the current block is even is throwing a sigseg fault and i'm not sure why.

void unzip_file(const std::string& filename) {
ifstream file(filename, ios::in | ios::binary | ios::ate);
streampos sz = file.tellg();
file.seekg(0, ios::beg);
char* input = new char[sz];
file.read(input, sz);

char* arrayOne = new char[sz / 2];
char* arrayTwo = new char[sz / 2];
char* finalArray = new char[sz];
int eo = sz / 4;
int idx = 0;

for (int i = 1; i <= eo; i++) {
    if (i % 2 != 0) {
        for (int x = 0; x < 4; x++) {
            arrayOne[idx] = input[idx];
            idx++;
        }
    }
    else {
        for (int y = 0; y < 4; y++) {
            arrayTwo[idx] = input[idx];
            idx++;
        }
    }
}

}

This is the array that's passed into the function

H   W   e   o   l   r   l   l   o   d   ,   !

Aucun commentaire:

Enregistrer un commentaire