为什么我的数组在将它传递给另一个函数后持有垃圾 values/segfaulting?
Why is my array holding garbage values/segfaulting after passing it into another function?
我有一个函数可以 malloc 一个数组来保存要编码到的值。在将值编码为整数数组后,该数组本身通过引用传递(至少理论上是这样)。然而,当我编译这个函数时,我得到了垃圾值,所以我开始到处扔打印语句来查看发生了什么。在调用第二个函数并传递数组之后,我意识到打印语句是段错误,这对我来说很奇怪。
主要功能:
void top_left_justify(char **pieces)
{
int i;
int j;
int count;
int *values; //array to be passed
i = 0;
if (!(values = (int *)malloc(sizeof(int) * 4)))
return ;
while (pieces[i] != 0)
{
j = 0;
count = 0;
while (pieces[i][j])
{
printf("count = %d\n", count);
if (pieces[i][j] == '#')
values[count++] = j;
j++;
}
printf("encoded indexes are: %d, %d, %d, %d\n", values[0],
values[1], values[2], values[3]); //test printout shows me it works here
top_left_helper(&values, pieces[i]); //PASS BY REFERENCE??
i++;
}
}
然而,在它被传递到这个函数之后:
void top_left_helper(int **values, char *pieces)
{
int i;
int j;
i = 0;
j = 0;
printf("value of values[0] = %d\n", *values[0]); //prints correct value
printf("value of values[0] = %d\n", *values[1]); //segfaults
left_helper(&*values);
top_helper(&*values);
while (i < 16)
pieces[i++] = '.';
while (j < 4)
pieces[*values[j++]] = '#';
}
它在第二个打印语句上出现段错误。除了我对通过引用和内存传递参数的工作方式的误解之外,我真的看不到对此的解释。请说明一下。
编辑:
经过一些测试,如果我通过递增指针而不是数组访问来打印出值,我似乎能够正确访问要打印的值。但这对我来说仍然没有任何意义,为什么数组访问不起作用。 (在第二个函数中添加了以下行)
printf("value of values[0] = %d\n", **values);
*values = *values + 1;
printf("value of values[1] = %d\n", **values);
*values = *values + 1;
printf("value of values[2] = %d\n", **values);
*values = *values + 1;
printf("value of values[3] = %d\n", **values);
这不是您认为的那样:
*values[1]
[]
的优先级高于 *
,因此这首先将 values
索引为数组,然后取消引用它返回的值。但是你传递给这个函数的是一个指向变量的指针,它也是一个指针。所以 values[1]
没有意义。你不用 *values[0]
因为它和 **values
一样 你想要的是:
(*values)[1]
但是,除非您需要修改 vaules
本身,例如在其上使用 realloc
,否则您不需要传递其地址。就像 pieces[i]
:
一样将其作为 int *
传递
void top_left_helper(int *values, char *pieces)
{
int i;
int j;
i = 0;
j = 0;
printf("value of values[0] = %d\n", values[0]);
printf("value of values[0] = %d\n", values[1]);
left_helper(values); // probably need to change left_helper the same way
top_helper(values); // and also top_helper
while (i < 16)
pieces[i++] = '.';
while (j < 4)
pieces[values[j++]] = '#';
}
然后相应地更改调用:
top_left_helper(values, pieces[i]);
我有一个函数可以 malloc 一个数组来保存要编码到的值。在将值编码为整数数组后,该数组本身通过引用传递(至少理论上是这样)。然而,当我编译这个函数时,我得到了垃圾值,所以我开始到处扔打印语句来查看发生了什么。在调用第二个函数并传递数组之后,我意识到打印语句是段错误,这对我来说很奇怪。
主要功能:
void top_left_justify(char **pieces)
{
int i;
int j;
int count;
int *values; //array to be passed
i = 0;
if (!(values = (int *)malloc(sizeof(int) * 4)))
return ;
while (pieces[i] != 0)
{
j = 0;
count = 0;
while (pieces[i][j])
{
printf("count = %d\n", count);
if (pieces[i][j] == '#')
values[count++] = j;
j++;
}
printf("encoded indexes are: %d, %d, %d, %d\n", values[0],
values[1], values[2], values[3]); //test printout shows me it works here
top_left_helper(&values, pieces[i]); //PASS BY REFERENCE??
i++;
}
}
然而,在它被传递到这个函数之后:
void top_left_helper(int **values, char *pieces)
{
int i;
int j;
i = 0;
j = 0;
printf("value of values[0] = %d\n", *values[0]); //prints correct value
printf("value of values[0] = %d\n", *values[1]); //segfaults
left_helper(&*values);
top_helper(&*values);
while (i < 16)
pieces[i++] = '.';
while (j < 4)
pieces[*values[j++]] = '#';
}
它在第二个打印语句上出现段错误。除了我对通过引用和内存传递参数的工作方式的误解之外,我真的看不到对此的解释。请说明一下。
编辑: 经过一些测试,如果我通过递增指针而不是数组访问来打印出值,我似乎能够正确访问要打印的值。但这对我来说仍然没有任何意义,为什么数组访问不起作用。 (在第二个函数中添加了以下行)
printf("value of values[0] = %d\n", **values);
*values = *values + 1;
printf("value of values[1] = %d\n", **values);
*values = *values + 1;
printf("value of values[2] = %d\n", **values);
*values = *values + 1;
printf("value of values[3] = %d\n", **values);
这不是您认为的那样:
*values[1]
[]
的优先级高于 *
,因此这首先将 values
索引为数组,然后取消引用它返回的值。但是你传递给这个函数的是一个指向变量的指针,它也是一个指针。所以 values[1]
没有意义。你不用 *values[0]
因为它和 **values
一样 你想要的是:
(*values)[1]
但是,除非您需要修改 vaules
本身,例如在其上使用 realloc
,否则您不需要传递其地址。就像 pieces[i]
:
int *
传递
void top_left_helper(int *values, char *pieces)
{
int i;
int j;
i = 0;
j = 0;
printf("value of values[0] = %d\n", values[0]);
printf("value of values[0] = %d\n", values[1]);
left_helper(values); // probably need to change left_helper the same way
top_helper(values); // and also top_helper
while (i < 16)
pieces[i++] = '.';
while (j < 4)
pieces[values[j++]] = '#';
}
然后相应地更改调用:
top_left_helper(values, pieces[i]);