添加变量导致的段错误

2023-12-25

诚然,我是一个纯 C 新手,但这让我难住了。我正在研究链表实现以进行练习,并且通过简单地将变量添加到 split_node 函数中,我遇到了段错误:

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

struct Node {
    struct Node *child;
    char *content;
};

void print_list(struct Node node);
void split_node(struct Node *node, int position);

int main() {

    struct Node head, second, third;

    head.content = "first";
    second.content = "second";
    third.content = "i'm third";

    head.child = &second;
    second.child = &third;

    print_list(head);
    split_node(&head, 3);
    print_list(head);

    return 0;
}

void print_list(struct Node node) {
    printf("%s\n", node.content);
    if(node.child) print_list(*node.child);
}

    /*
    Split node into two nodes, with the first position characters of the node's content remaining with node, and the remainder being copied to the new node. (It doesn't yet truncate the first node's string, but does do the copy.)
    */
void split_node(struct Node *node, int position) {
    if(position >= strlen((*node).content)) return;
    struct Node newNode;
    newNode.child = (*node).child;
    (*node).child = &newNode;

    int length = (strlen((*node).content) - position);
    newNode.content = malloc(sizeof(char) * (length + 1));
    strncpy(newNode.content, (*node).content + sizeof(char) * position, length);
    newNode.content[length] = '\0';

    //int foo;
}

此代码编译(gcc -Wall -o list list.c)并运行良好:

$ ./list
first
second
i'm third
first
st
second
i'm third

但如果我取消评论int foo在......的最后split_node,编译并运行,我得到:

$ ./list
first
second
i'm third
first
st
Segmentation fault

gdb 给了我这个回溯:

#0  0x91d6ae70 in strlen ()
#1  0x91dd3126 in puts ()
#2  0x00001f21 in print_list (node={child = 0xbcec815b, content = 0x8b000000 <Address 0x8b000000 out of bounds>}) at list.c:41
#3  0x00001f3c in print_list (node={child = 0x8fe0154b, content = 0x1ff6 "i'm third"}) at list.c:42
#4  0x00001f3c in print_list (node={child = 0xbffff568, content = 0x1fef "second"}) at list.c:42
#5  0x00001f3c in print_list (node={child = 0xbffff570, content = 0x1fe9 "first"}) at list.c:42
#6  0x00001ee0 in main () at list.c:33

为什么添加变量定义会导致段错误?它似乎正在破坏新创建节点的内容指针。我很困惑;有什么帮助吗?


您需要动态分配节点(使用 malloc)。

正如您所看到的,您的新节点已在堆栈上声明。当 split 函数返回时,该新节点不再是有效内存。

添加变量会导致段错误,因为该变量会更改堆栈的布局,从而导致函数返回时行为略有不同。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

添加变量导致的段错误 的相关文章

随机推荐

  • 不再有 PHP 5.3.x 的 VC6 版本吗?

    我需要在运行 Apache 的 Windows 机器上安装 PHP 5 3 x 最好是 x gt 4 不幸的是 在PHP 下载页面 http windows php net download 我找不到任何用VC6编译的PHP 5 3 x 所
  • “您尚未指定用作弹出窗口内容的视图”

    所以我一直在尝试在我的游戏中实现排行榜 成就 并且在没有任何类型的实现的情况下 游戏运行得很好 我已成功导入 google play services lib 并复制到 BaseGameUtils 但每当我尝试调用 GameHelper 对
  • Service Fabric 服务远程处理

    过去几周我一直在从云服务迁移到 Service Fabric 并且在两个服务之间使用远程处理时遇到了一些障碍 我一直在使用服务远程处理的官方文档和示例代码 特别是我试图让此处概述的示例正常工作 https learn microsoft c
  • 如何删除 wp 短代码中的
     标签?                
                

    我正在尝试创建一个短代码来显示最新的帖子 我使用了以下代码作为短代码 function my recent posts shortcode atts extract shortcode atts array limit gt 5 atts
  • Spark Streaming mapWithState 超时延迟?

    我预计 Spark 1 6 的新 mapWithState API 能够几乎立即删除超时的对象 但存在延迟 我正在使用改编版本测试 APIJavaStatefulNetworkWordCount https github com apach
  • 如何制作动画到wrap_content?

    是否可以使用动画ValueAnimator to wrap content 这似乎只适用于恒定值 public static void valueAnimate final View obj int from int to Interpol
  • 集中回滚-用于使用@transactional

    是否可以告诉Spring回滚异常MyException也RuntimeException使用时在 XML 配置中 transactional 我知道可以在注释中设置回滚 但如果我有很多服务都设置相同的异常 那么这似乎是多余的 我看到人们建议
  • JUnit 5:指定嵌套测试的执行顺序

    是否可以以固定的执行顺序在其他一些测试之间执行多个嵌套测试 E g TestInstance Lifecycle PER CLASS TestMethodOrder OrderAnnotation class class MyTest pr
  • POSIXct 日期转换错误[重复]

    这个问题在这里已经有答案了 将一组字符格式的日期转换为 POSIXct 对象时 我遇到了以下错误 示例数据 t lt c 3 11 2007 1 30 3 11 2007 2 00 4 11 2007 2 00 str t chr 1 3
  • 如何使用区域设置获取特定国家/地区的货币符号?

    我已经尝试过这段代码 它给了我Country Code对于某些国家而不是currency symbol 我想要货币符号而不是代码 数组 resourcesList 包含所有具有其代码的国家 地区 String m String Array
  • 如何在 Android 应用程序中指定和添加自定义打印机?

    我正在为 Android 创建一个应用程序 所需的应用程序功能的一部分是用户可以选择一个特殊的打印机 我们将其称为传输打印机 它将将要打印的文档传递到在外部服务器上运行的进程 我需要采取哪些步骤才能将自定义打印机添加到 Android 打印
  • 双包含解决方案?

    在 C 中 我遇到了双重包含的问题 文件 stuffcollection h pragma once ifndef STUFFCOLLECTION H define STUFFCOLLECTION H include Stage h cla
  • Tensorflow、try 和 except 不处理异常

    我是张量流的新手 我在这里遇到了一个恼人的问题 我正在制作一个程序 加载使用以下命令拍摄的图像 原始数据 tf WholeFileReader read image name queue 从 tfrecord 文件中读取 然后使用tf im
  • 在同一表达式中调用具有局部副作用的函数两次是否是未定义的行为?

    int f static int i 0 return i int g return f f Does g return 3或者是结果undefined 章节和诗句 http www open std org jtc1 sc22 wg14
  • 属性 insetForeground 已经定义

    更新到新版本后 com android support design 22 2 0 我收到这个错误 属性 insetForeground 已经定义 请记住 我正在使用 romannurikScrimInsetsFrameLayout jav
  • Ruby on Rails,找不到有效的 gem 'rails'

    我安装了 ruby 并更新了 ruby gems 现在我想下载 Rails 3 2 13 我写 gem install Rails v 3 2 13 我需要这个版本 我有这个错误 ERROR Could not find a valid g
  • 为什么我们需要在Python中进行编码和解码?

    编码 解码的用例是什么 我的理解是 编码用于将字符串转换为字节字符串 以便能够在程序中传递非 ascii 数据 而decode就是将这个字节串转换回字符串 有点遵循 示例显示即使未编码 解码 非 ascii 字符也能成功打印 例子 val1
  • 在 PDF 中插入换行符

    我正在使用 PHP 即时生成一些 PDF 文件 我的问题是我需要在将插入 PDF 文件的文本的某些部分插入换行符 就像是 pdf gt InsertText Line one n nLine two 所以它打印 Line one Line
  • Visual Studio 2015 非常慢

    我刚安装完 整个IDE速度超级慢 看起来它正在后台进行某种繁重的 CPU 调用 整个 IDE 几乎冻结并在大约 2 3 秒内变得无响应 我在使用 Visual Studio 2013 Ultimate 时没有遇到此问题 我正在运行 Visu
  • 添加变量导致的段错误

    诚然 我是一个纯 C 新手 但这让我难住了 我正在研究链表实现以进行练习 并且通过简单地将变量添加到 split node 函数中 我遇到了段错误 include