引用改变大小的向量时出现分段错误

发布于 2025-01-14 23:03:44 字数 1530 浏览 3 评论 0原文

因此,我在主函数中初始化一个向量,并将该向量作为引用传递到其大小发生变化的函数中。现在,我想象主函数中初始化的向量的大小会改变,但事实并非如此。我只是错误地执行了此操作吗?

#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);
    }
}

编辑: 这就是正在发生的事情。当您创建向量时, I 5 5 调用 createImage() ,这是可行的,但当您 L 4 4 P 调用 myVector[4][4] 处的 colorPixel() 时,它开始失败。

如果我将 main 中的 myVector 初始化为 250 250,则 print 将打印所有 250 行,即使在 createImage() 中更改了大小(例如 4 4)之后也是如此。

简而言之,我相信并且确信,main 中的向量没有得到其大小已更改的提示,是否有任何解决方案?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无敌元气妹 2025-01-21 23:03:44
    while(std::cin >> operation){
        vector<vector<char> > myVector;

在此 while 循环中发生的第一件事是创建一个新的 myVector。从字面上看,这就是 C++ 中的内容。

当到达循环末尾时,此 myVector 将被销毁。这就是 C++ 的工作原理:如果您在循环中或在 if 语句内声明一个对象,则当执行到达此范围的末尾时,该对象将被销毁。

因此,当这个 while 循环到达终点时,这个 myVector 就会被销毁。它不再存在了。它将会消失。它将轻松存在。它将渴望峡湾。它将成为前 myVector。

然后,再次检查 while 循环的条件,如果条件仍然成立,则执行再次进入 while,创建一个新的 myVector,从头开始。无论如何,它与之前的 myVector 没有任何关系,后者只是一个微弱的记忆。

解决这个问题的方法是在 main 的开头声明这个 myVector 之前这个循环,因此,只有当 main 本身完成执行时,这个 myVector 才会被销毁。

    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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文