c - Valgrind malloc 在 C 中无效的写入和读取
问题描述
我用 C 编写了以下程序。
#include<stdio.h>
#include<stdlib.h>
struct stack{
int *arr;
int top;
};
struct stack* init(int size){
struct stack *s=NULL;
s=malloc(sizeof(struct stack));
s->arr=malloc(sizeof(int) * (size));
s->top=-1;
return s;
}
void push(struct stack* t, int val){
++(t->top);
t->arr[t->top]=val;
}
void pop(struct stack* t){
if(t->top != -1)
t->top--;
}
int peek(struct stack *t){
if(t->top != -1)
return t->arr[t->top];
return -1;
}
int isEmpty(struct stack *t){
if(t->top == -1)
return 1;
return 0;
}
int adjVisited(int n, int **g, int i){
for(int j=0;j<n;j++){
if(g[i][j] == 1)
return 0;
}
return 1;
}
void Euler(int v, int n, int **g, int e){
struct stack *temp=init(e+2);
struct stack *ans=init(e+2);
push(temp, v);
while(!isEmpty(temp)){
int u=peek(temp);
if(adjVisited(n, g, u)){
push(ans, u);
pop(temp);
}
else{
for(int j=0;j<n;j++){
if(g[u][j] == 1){
push(temp, j);
g[u][j] = 0;
g[j][u] = 0;
break;
}
}
}
}
free(temp->arr);
free(temp);
while(!isEmpty(ans)){
printf("%d ", peek(ans));
pop(ans);
}
}
void addEdge(int n, int** g, int u, int v){
g[u][v]=1;
g[v][u]=1;
}
int main(){
int n, e, x, y;
printf("Enter number of nodes in the graph\n");
scanf("%d", &n);
int **graph = (int **)calloc(n, sizeof(int *));
for (int i=0; i<n; i++)
graph[i] = (int *)calloc(n, sizeof(int));
printf("Enter number of edges in the graph\n");
scanf("%d", &e);
printf("Enter end vertices of the edge to be added\n");
while(e--){
scanf("%d %d", &x,&y);
addEdge(n, graph, x, y);
}
Euler(0, n, graph, e);
for(int i=0;i<n;i++)
free(graph[i]);
free(graph);
return 0;
}
Valgrind 向我抛出无效写入和无效读取错误。这是我第一次遇到这个错误。据我所知,每当我想将数据写入尚未动态分配的内存位置时,就会发生这种情况。
==4199== Memcheck, a memory error detector
==4199== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==4199== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==4199== Command: ./a.out
==4199==
Enter number of nodes in the graph
6
Enter number of edges in the graph
6
Enter end vertices of the edge to be added
0 1
1 2
2 3
3 4
4 5
5 0
==4199== Invalid write of size 4
==4199== at 0x109294: push (hier.c:31)
==4199== by 0x109477: Euler (hier.c:75)
==4199== by 0x1096D4: main (hier.c:114)
==4199== Address 0x4a52bc4 is 0 bytes after a block of size 4 alloc'd
==4199== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==4199== by 0x10923E: init (hier.c:24)
==4199== by 0x1093AA: Euler (hier.c:60)
==4199== by 0x1096D4: main (hier.c:114)
==4199==
==4199== Invalid read of size 4
==4199== at 0x1092F4: peek (hier.c:41)
==4199== by 0x1093F2: Euler (hier.c:66)
==4199== by 0x1096D4: main (hier.c:114)
==4199== Address 0x4a52bc4 is 0 bytes after a block of size 4 alloc'd
==4199== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==4199== by 0x10923E: init (hier.c:24)
==4199== by 0x1093AA: Euler (hier.c:60)
==4199== by 0x1096D4: main (hier.c:114)
==4199==
==4199== Invalid write of size 4
==4199== at 0x109294: push (hier.c:31)
==4199== by 0x10941E: Euler (hier.c:69)
==4199== by 0x1096D4: main (hier.c:114)
==4199== Address 0x4a52c64 is 0 bytes after a block of size 4 alloc'd
==4199== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==4199== by 0x10923E: init (hier.c:24)
==4199== by 0x1093CC: Euler (hier.c:62)
==4199== by 0x1096D4: main (hier.c:114)
==4199==
==4199== Invalid read of size 4
==4199== at 0x1092F4: peek (hier.c:41)
==4199== by 0x10951A: Euler (hier.c:86)
==4199== by 0x1096D4: main (hier.c:114)
==4199== Address 0x4a52c78 is 20 bytes after a block of size 4 alloc'd
==4199== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==4199== by 0x10923E: init (hier.c:24)
==4199== by 0x1093CC: Euler (hier.c:62)
==4199== by 0x1096D4: main (hier.c:114)
==4199==
编辑:测试以下输入时出现此错误
6 as number of vertices
9 as number of edges
0 1
1 2
2 3
3 4
4 5
5 0
0 2
2 4
0 4 as the edges of the graph
谁能帮我解决这个错误?
解决方案
e
在您的示例中,您将边数分配给变量e == 9
。
然后你有一个while循环,你e
在读取边缘时会减少:
while (e--)
{ ... }
之后e
会-1
。
你Euler
用e==-1
作为参数调用。arr
您为元素分配内存-1 + 2 == 1
,这对于 9 条边来说是不够的。
在第二次调用push
您时,尝试写入未分配的元素索引1
。arr
因此 valgrind 将您写入的大小为 4的地址重新发布到大小为 4 的块之后的 0 字节的地址。
推荐阅读
- amp-html - AMP URL API 未返回有效的 AMP 页面
- android - 在 ABI 的 NDK 工具链文件夹中找不到工具链,前缀为:Android Studio 中的 mipsel-linux-android 错误
- python-3.x - 在 GCP 功能中使用 Flask 路由?
- keras - Keras 功能 API:将 CNN 模型与 RNN 结合起来查看图像序列
- python - 更快地关闭 tkinter 窗口(当有很多按钮时)
- c# - 合并两个列表并替换元素
- php - 更改 PHP 梨邮件中的电子邮件标题
- python - 为什么我的标签没有显示在 tkinter 中?
- python - Setuptools:使用源文件夹中的 setup.py 从“项目”导入
- javascript - 使用javascript在Adobe中根据大小删除空白页