Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;三维向量更新malloc校验和不正确 我刚刚学习了来自Python的C++。我对内存管理概念非常陌生,我很难理解我的方法出了什么问题。我的目标是从用户输入中获取一个数字列表,并将它们放在NxN矢量图中。每个节点都是一个向量,存储{input\u number,row\u index,col\u index,0}。有人能解释一下这里到底出了什么问题吗?for循环似乎到达第二行,然后出错_C++_Vector_Memory Management_Malloc - Fatal编程技术网

C++;三维向量更新malloc校验和不正确 我刚刚学习了来自Python的C++。我对内存管理概念非常陌生,我很难理解我的方法出了什么问题。我的目标是从用户输入中获取一个数字列表,并将它们放在NxN矢量图中。每个节点都是一个向量,存储{input\u number,row\u index,col\u index,0}。有人能解释一下这里到底出了什么问题吗?for循环似乎到达第二行,然后出错

C++;三维向量更新malloc校验和不正确 我刚刚学习了来自Python的C++。我对内存管理概念非常陌生,我很难理解我的方法出了什么问题。我的目标是从用户输入中获取一个数字列表,并将它们放在NxN矢量图中。每个节点都是一个向量,存储{input\u number,row\u index,col\u index,0}。有人能解释一下这里到底出了什么问题吗?for循环似乎到达第二行,然后出错,c++,vector,memory-management,malloc,C++,Vector,Memory Management,Malloc,源代码: int N; cin >> N; int total = N * N; vector<vector<vector<int>>> graph (N, vector<vector<int>>(N, vector<int>(4))); int col = 0; int row = 0; int startcol; int startrow; for (int i = 0; i < total; i++)

源代码:

int N;
cin >> N;
int total = N * N;
vector<vector<vector<int>>> graph (N, vector<vector<int>>(N, vector<int>(4)));
int col = 0; int row = 0; int startcol; int startrow;
for (int i = 0; i < total; i++) { 
    int num;
    cin >> num;
    graph[row][col] = vector<int>({num, row, col, 0});
    if (num == 1) {
        startrow = row;
        startcol = col;
    }
    if (col < N) {
        col++;
    }
    else {
        col = 0;
        row++;
    }
}
int result = search(graph, startrow, startcol, N);

我最终使用了一种不同的方法,嵌套for循环,并一次更新节点的每个元素。我感觉这个问题与覆盖图[row][col]=vector({num,row,col,0})行中的整个节点向量有关,但在Python中这会非常好地工作(可能是因为它处理了bg中的所有内存管理),所以我不明白为什么我不能用新整数的N向量替换0的N向量。

在当前逻辑中,
col
检查是在访问
graph[row][col]
后完成的。这意味着
col
变为等于
N
,尝试访问
图形[0][N]
,并且仅在重置
col
之后


因此,您必须在访问
图形之前检查
col
,或者在访问之后检查“if(col 我无法重现你的错误,你能提供一个每次都失败的完整例子吗。这里是我尝试过的:哦,哇,我觉得自己很愚蠢。这太明显了,我简直不敢相信我没抓到。我忙着想这是件非常复杂的事情。而且我已经习惯了一些不那么神秘的消息,比如“IndexError:list assignment index out-range”,这一点很明显,哈哈。非常感谢。
a.out(7393,0x7fffc20433c0) malloc: *** error for object 0x100300138: incorrect checksum for freed object - object was probably modified after being freed.