c - 为什么会出现段错误? (少量代码)

标签 c

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

#include "mystuff.h"

typedef struct Node
{
        int size;
        int status;
        struct Node *next;
        struct Node *previous;
} Node;


Node *endNode;
Node *rootNode;

void *my_foo(int size)
{
        Node *theNode;
        void *ptr;

        if (rootNode->next == NULL)
        {
                theNode->status = 1;
                ...

它在“rootNode-> == NULL”处出现段错误,但如果我将其更改为“rootNode == NULL”,那么当它遇到“theNode->status = 1”时就会出现段错误,并将 theNode 和 rootNode 声明为 NULL 没有帮助.

最佳答案

...and declaring theNode and rootNode as NULL does not help.

取消引用NULL(通常)导致段错误。在开始修改它们之前,您必须将它们设置为更有用的东西(例如分配的结构)!

关于c - 为什么会出现段错误? (少量代码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15891842/

相关文章:

c - 逻辑运算符 | |在 C 中

connect() 在阻塞套接字上返回 "Operation now in progress"?

c - C 中的 HTTP 代理,recv() 不会阻塞?

c - 从 Linux 移植到 OSX 时,时间和 ctime 出现段错误

C - 套接字无法连接到 SMTP 服务器

c - 使用 VS Studio 调试非常大的文件

c - 在结构中初始化 main vs 中的 char 数组

c - 为什么编译器认为我在 "pow"中使用 double, 。 'double' 到 'float' C4244

CUDA 和 if else if

c - 生成 1 和 0 并以菱形形式存储在数组内