为什么会出现这样的故障?
我在 C 中有这个程序:
#include <stdio.h>
#include <string.h>
#include <stdint.h>
typedef struct sl{
int32_t length;
int32_t* arr;
} Selector;
void somefunction(Selector* temp){
temp->length = 10;
temp->arr = (int32_t*)malloc(temp->length * sizeof(int32_t));
for(int i=0; i<temp->length; i++){
temp->arr[i] = i*i;
}
}
int main () {
Selector* sel;
// Make changes to struct from other function
somefunction(sel);
// Print each element
for(int i=0; i<sel->length; i++){
printf("Content of index %d: %d\n",i,sel->arr[i]);
}
printf("\n");
return(0);
}
我在 PowerShell 中运行它: gcc .\stest.c; .\a.exe
,并且它工作正常:
Content of index 0: 0
Content of index 1: 1
Content of index 2: 4
Content of index 3: 9
Content of index 4: 16
Content of index 5: 25
Content of index 6: 36
Content of index 7: 49
Content of index 8: 64
Content of index 9: 81
但是如果我将 int main()
更改为:
int main () {
Selector* sel;
// Make changes to struct from other function
somefunction(sel);
// Print each element
for(int i=0; i<sel->length; i++){
printf("Content of index %d: %d\n",i,sel->arr[i]);
}
printf("\n");
// ============= ADDED CODE BELOW ============= //
// Change each element a bit
for(int i=0; i<sel->length; i++){
sel->arr[i] = sel->arr[i] + 10;
}
// Print each element again
for(int i=0; i<sel->length; i++){
printf("Content of index %d after change: %d\n",i,sel->arr[i]);
}
printf("\n");
// ============= ADDED CODE ABOVE ============= //
return(0);
}
突然它会出现分段错误?为什么?我没有使用堆栈并重载它,我对小数组使用了 malloc,它不会再次传递引用,就像传递到函数中一样导致内存丢失或其他什么。为什么这不起作用?否则我该怎么做呢?
I have this program in C:
#include <stdio.h>
#include <string.h>
#include <stdint.h>
typedef struct sl{
int32_t length;
int32_t* arr;
} Selector;
void somefunction(Selector* temp){
temp->length = 10;
temp->arr = (int32_t*)malloc(temp->length * sizeof(int32_t));
for(int i=0; i<temp->length; i++){
temp->arr[i] = i*i;
}
}
int main () {
Selector* sel;
// Make changes to struct from other function
somefunction(sel);
// Print each element
for(int i=0; i<sel->length; i++){
printf("Content of index %d: %d\n",i,sel->arr[i]);
}
printf("\n");
return(0);
}
I run it in PowerShell with: gcc .\stest.c; .\a.exe
, and it works fine:
Content of index 0: 0
Content of index 1: 1
Content of index 2: 4
Content of index 3: 9
Content of index 4: 16
Content of index 5: 25
Content of index 6: 36
Content of index 7: 49
Content of index 8: 64
Content of index 9: 81
But if I change int main()
to this:
int main () {
Selector* sel;
// Make changes to struct from other function
somefunction(sel);
// Print each element
for(int i=0; i<sel->length; i++){
printf("Content of index %d: %d\n",i,sel->arr[i]);
}
printf("\n");
// ============= ADDED CODE BELOW ============= //
// Change each element a bit
for(int i=0; i<sel->length; i++){
sel->arr[i] = sel->arr[i] + 10;
}
// Print each element again
for(int i=0; i<sel->length; i++){
printf("Content of index %d after change: %d\n",i,sel->arr[i]);
}
printf("\n");
// ============= ADDED CODE ABOVE ============= //
return(0);
}
Suddenly it just gets a segmentation fault? Why? I didn't use the stack and overload it, I used malloc for the small arrays, it's not passing around references again like into the function to make memory go missing or something. Why doesn't this work? And how am I supposed to do it otherwise?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
感谢评论中给出解决方案的人。
该问题是由未定义的行为引起的,因为指针未设置为任何值。
总结一下,对于后代和其他遇到同样问题的人来说:
Credit goes to the guys in the comments who gave the solution.
The problem was caused by undefined behavior, because the pointer was not set to anything.
To summarize it, for posterity and others running into the same problem: