pointers - 将整数分配给指针时出现分段错误
问题描述
我正在尝试将我的节点值分配给一个指针,但是当代码运行时 gdb 给了我分段错误。我能做些什么?
void biggerPotion(No* node, int bottleSize, int *aux){
if(node == NULL)
return;
maiorPocao(node>left, bottleSize, aux);
maiorPocao(node->right, bottleSize, aux);
if((node->value >= garra) && (node-> value < *aux))
*aux = node->value; //here is the issue
}
代码的其他相关部分是:
for(i=0; i< nBottles;i++){
a = 1000; //i declared that
biggerPotion(potions,bottleSize[i],&a);
}
解决方案
好的,因为错误的行是:
*aux = node->value;
那么要么aux
是问题要么node
是(因为它们是该行上唯一被取消引用的两个指针)。
if
为了确定,我会在执行该块之前将它们都打印出来:
fprintf(stderr, "node is %p, aux is %p\n", node, aux);
考虑到. node
_ aux
_ _ 您应该发布该顶级调用,包括您传入的任何变量的声明。biggerPortion
在任何情况下,您都可以通过简单地更改来测试:
*aux = node->value;
进入:
{
int temp = node->value;
}
如果问题消失了,那么肯定是aux
指针以某种方式出错了。确保您实际上是在传递一个指针,例如:
int myVar;
biggerPotion(rootNodePtr, 42, &myVar);
推荐阅读
- solrcloud - solr 上的连接被拒绝错误
- java - 有没有办法在流中使用多个 allMatch() 而不是使用下面的形式?
- python - Pythonic 方法来找出最小的 2D 平面(框)以适合所有部分(最大的部分)
- android - 带有 Cordova 应用程序的 Ionic3 在某些 android 11 中随机崩溃
- java - 如何使用打开的相机android在相机预览屏幕中添加镜像效果
- sql - 防止在表更新时同时触发多个触发器
- java - 使用 Apache POI 在 Excel 中嵌入 PDF 和 Word 文档
- javascript - 如何将数据传递到 chartjs 标签?
- cypress - 赛普拉斯:如果条件满足,我需要退出 for 循环
- java - 在java Android中检查字符串是否仅包含数字和空格