Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 在函数中切换对printf的调用如何确定我的代码是否';不要撞车?_C - Fatal编程技术网

C 在函数中切换对printf的调用如何确定我的代码是否';不要撞车?

C 在函数中切换对printf的调用如何确定我的代码是否';不要撞车?,c,C,我正在编写一个简单的函数,在链表的开头创建一个新节点。当我尝试执行该文件时,我的Windows命令错误窗口出现,显示“a.exe已停止工作。” 对我来说,一个谜是,当我在insertnewnodeathfront的主体中添加printf调用时,代码执行时没有上面的错误框。想法 以下是源文件: #include <stdio.h> typedef struct NodeTag{ const char* Airport; struct NodeTag * Link;

我正在编写一个简单的函数,在链表的开头创建一个新节点。当我尝试执行该文件时,我的Windows命令错误窗口出现,显示“a.exe已停止工作。”

对我来说,一个谜是,当我在
insertnewnodeathfront
的主体中添加
printf
调用时,代码执行时没有上面的错误框。想法

以下是源文件:

#include <stdio.h>


typedef struct NodeTag{
    const char* Airport;
    struct NodeTag * Link;                
    } NodeType;


void insertNewNodeAtFront(NodeType *, const char*);


void insertNewNodeAtFront(NodeType * L,  const char* str){

    NodeType * N;
    N = L;    
    NodeType * NewFirst;
    NewFirst->Airport = str;
    NewFirst->Link = N;
    L = NewFirst;
   //printf("L->Airport: %s\n",L->Airport); <---This is the line that magically makes it work.
}

int main(){
    NodeType * myitinerary;
    insertNewNodeAtFront(myitinerary,"ONT");
    return 0;
}
#包括
类型定义结构节点tag{
康斯特查尔机场;
结构节点表*链接;
}节点类型;
void insertNewNodeAtFront(节点类型*,常量字符*);
void insertNewNodeAtFront(节点类型*L,常量字符*str){
节点类型*N;
N=L;
NodeType*NewFirst;
新建第一->机场=str;
NewFirst->Link=N;
L=NewFirst;

//printf(“L->Airport:%s\n”,L->Airport);代码正在延迟单元化指针,导致:

在使用前为
NewFirst
分配内存:

NodeType* newFirst = malloc(sizeof(*newFirst));
if (newFirst)
{
}
printf()
在维护未定义行为方面起着主导作用,但不是原因

此外,如果希望调用方能够看到对
L
的更改,则需要在C通过值时传入指向
insertNewNodeAtFront()
的指针地址(请参阅):


当您的程序有未定义的行为时,各种事情都可能发生

具体地说,您从来没有为节点实际分配内存(使用
malloc
),因此当您开始执行类似
NewFirst->Airport=str;
-它会覆盖内存中的随机地址(NewFirst是一个未初始化的指针)。所有赌注均已取消。在继续执行实现尝试之前,您可能应该搜索实现链接列表的示例代码。

而不是此代码

 void insertNewNodeAtFront(NodeType * L,  const char* str){

        NodeType * N;
        N = L;    
        NodeType * NewFirst;
        NewFirst->Airport = str;
        NewFirst->Link = N;
        L = NewFirst;
       //printf("L->Airport: %s\n",L->Airport); <---This is the line that magically makes it work.
    }
主要功能

insertNewNodeAtFront(&myitinerary,"ONT");

正如hmjd所说,崩溃是由使用未初始化的内存引起的。当您添加更多代码时,它不会在该点崩溃的原因是,这会导致编译器在内存中移动内容,因此未初始化的变量指向不同的位置


但是,这仍然很糟糕:您的代码仍然清除了内存中的一些随机位置,这很可能会导致下次崩溃。

调试器会怎么说?您如何知道“a.exe已停止工作?”是一个错误吗?我还没有在这个问题上运行调试器。你建议基于Windows的C系统使用哪种调试器?@marounnaroun很好!我只是假设我的新手程序员状态是:)。我不相信marounnaroun有真正的意义。这很明显是一个错误,除非你打算在是你写的…根据定义。我知道在内存中取消引用一个我没有分配的指针会让我觉得很有趣。在这一行中,
NodeType*myinventurey;insertNewNodeAtFront(myinventurey,“ONT”);
你要传递什么?哪个值?在这一行中,你只传递一些垃圾值(未定义)
if(NewFirst)
检查分配是否成功?C不保证malloc将返回非空指针?@user2276081,是,它检查它不是
NULL
。否,
malloc()
不能保证成功,如果失败,它将返回
NULL
。在使用它之前,你应该查阅
malloc
的文档。我不确定什么文档适合你在GNU/Linux机器
man malloc
上使用的任何C实现(除非你决定对违反C标准的C实现使用一些疯狂的编译器)。如果你能找到你正在使用的C实现的文档,那么就把它作为你的参考。
 void insertNewNodeAtFront(NodeType * L,  const char* str){

        NodeType * N;
        N = L;    
        NodeType * NewFirst;
        NewFirst->Airport = str;
        NewFirst->Link = N;
        L = NewFirst;
       //printf("L->Airport: %s\n",L->Airport); <---This is the line that magically makes it work.
    }
void insertNewNodeAtFront(NodeType ** L,  const char* str){


    NodeType * NewFirst = malloc(sizeof(NodeType));
    NewFirst->Airport = str;
    NewFirst->Link = *L;
    *L = NewFirst;
    printf("L->Airport: %s\n",(*L)->Airport);
}
insertNewNodeAtFront(&myitinerary,"ONT");