简体   繁体   English

scanf处的指针分割错误

[英]pointer segmentation fault at scanf

a Little help? 一点帮助? I'm pretty sure the answer must be something silly, but I cannot see why am I getting a segmentation fault right after my scanf. 我很确定答案肯定是愚蠢的,但是我看不出为什么我的scanf之后立即出现分割错误。 I've been staring at my piece of code for a long time now: 我已经盯着我的代码很长时间了:

#include<stdio.h> 
#include<stdlib.h> 
#include<string.h> 

typedef struct Student{ 

    int grade, id; 
    struct Student *next; 
}student; 


student *initialize(){
    return NULL;
}

int main(){ 
    student *init; 
    student *nxt; 
    student *actual;
    int resp;

    init = (student*)malloc(sizeof(student)); 
    init = initialize();

    actual = init; 

    while(1){ 
        printf("type grade:\n"); 
        scanf("%d", &actual->grade); 
        printf("type id:\n"); 
        scanf("%d", &actual->id); 
        printf("wanna continue? (1-YES e <other>-NO)\n"); 
        if(resp==1){ 
            actual->next=(student*)malloc(sizeof(student)); 
            nxt=actual->next; 
        }else 
            break; 
    }

    actual->next=NULL;
    return 0; 
}

No big deal, right? 没关系吧? There is a struct, I want to scan a value into it. 有一个结构,我想扫描一个值。 on my terminal, I get: 在我的终端上,我得到:

type grade:
3
Segmentation fault (core dumped)

any ideas? 有任何想法吗?

First you allocate memory for init 首先,您为init分配内存

init = (student*)malloc(sizeof(student)); 

but you immediately set init to NULL with the return value of your initialize() function here 但是您立即使用initialize()函数的返回值将init设置为NULL

init = initialize();

Not only is this a memory leak, but you next set actual to NULL here 这不仅是内存泄漏,而且您接下来在此处actual设置为NULL

actual = init; 

Then later in your while loop you dereference actual (which is NULL) in several places such as here 然后,在稍后的while循环中,您在多个位置(例如此处)取消引用actual (为NULL)

scanf("%d", &actual->grade); 

Dereferencing NULL pointers is undefined behaviour and this is a likely source of your error. 取消引用NULL指针是未定义的行为,这可能是错误的来源。

Your initialize() function is returning null and you are creating a memory leak. 您的initialize()函数返回null,并且正在创建内存泄漏。 Instead of returning null initialize the data members to reasonable values. 而不是返回null,而是将数据成员初始化为合理的值。

"Thank you! I saw that you should initialize your structure as null on a tutorial, I guess I did it on the wrong order.. Noobs right? haha I'm sorry about the dumb question, you guys are the best" “谢谢!我看到您应该在教程中将结构初始化为null,我想我是按错误的顺序将其初始化的。.菜鸟吗?哈哈,我对这个愚蠢的问题感到抱歉,你们是最好的”

you are correct, you should initialize to null. 您是正确的,应将其初始化为null。 But the code you have doesnt do that. 但是您拥有的代码无法做到这一点。

You need to do this 你需要这样做

student *initialize(student * st)
{
   st->id = 0;
   st->grade = 0;
   st->next = NULL;
   return st;
}

or use calloc instead of malloc to make a student object (calloc clears the memry to 0) 或者使用calloc而不是malloc来创建一个学生对象(calloc将内存清除为0)

or do 或做

init = malloc ...
memset(init, 0, sizeof(student));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM