Home > Back-end >  Segmentation Fault when refrencing vectors that changed size
Segmentation Fault when refrencing vectors that changed size

Time:03-18

So I initialize a vector within a main function, and pass that vector as a reference into a function where its size changes. Now, I would imagine that the initialized vector within the main would change in size, but its not. Am I just implementing this incorrectly?

#include <iostream>
#include <vector>
using std::vector;

void createImage(vector<vector<char> >& img){
    int cols, rows;
    std::cin >> rows >> cols;
    img.clear();
    for(int i = 0; i < rows; i  )
        img.push_back(vector<char>(cols, 'O'));
}

void colorPixel(vector<vector<char> > &img){
    int xCoord, yCoord;
    char color;
    std::cin >> xCoord >> yCoord >> color;
    img[xCoord][yCoord] = color;
}

void printVector(vector<vector<char> > &img){
        for(int i = 0; i < img.size(); i  ){
            for(int j = 0; j < img[i].size(); j  )
                std::cout << img[i][j];
            std::cout << '\n';
        }
}

int main(){

    char operation;
    
    while(std::cin >> operation){
        vector<vector<char> > myVector;
        if(operation == 'X')
            break;
        if(operation == 'I')
            createImage(myVector);
        if(operation == 'C')
            printVector(myVector);
        if(operation == 'L')
            colorPixel(myVector);
    }
}

Edit: This is what is effectively happening. When you create the vector so I 5 5 which calls createImage(), that works, but it begins to fail when you L 4 4 P which calls colorPixel() at myVector[4][4].

If I were to initialize myVector in main to 250 250, then print will print all 250 lines, even after being changed in size (such as 4 4) in createImage().

In short, I believe, and know for sure, that the vector within main is not getting the hint that its size has changed, is there any fix for this?

CodePudding user response:

    while(std::cin >> operation){
        vector<vector<char> > myVector;

The very first thing that happens in this while loop is that a new myVector gets created. This is, literally, what this says in C .

When the end of the loop gets reached, this myVector gets destroyed. That's how C works: if you declare an object in a loop, or inside the if statement, the object gets destroyed when execution reaches the end of this scope.

So, when this while loop reaches this end, this myVector gets destroyed. It becomes no more. It will will be gone. It will ease to exist. It will be pining for the fjords. It will become an ex-myVector.

Then, the while loop's condition gets checked again, and if the condition still holds true, execution enters the while again, creating a new myVector, from scratch. It will have nothing to do, whatsoever, with the previous myVector, which is just a faint memory.

And the way to fix this is to declare this myVector before this loop, at the beginning of your main, so this myVector gets destroyed only when main, itself, finishes executing.

  • Related