gpt4 book ai didi

C - 将 "composited/inherited"结构实例传递给其基本类型的指针

转载 作者:行者123 更新时间:2023-11-30 20:30:48 26 4
gpt4 key购买 nike

所以,我有 2 个结构。

第一个:

typedef struct AST_NODE_STRUCT {
token* tok;
} ast_node;

另一个:

typedef struct AST_BINOP_STRUCT {
ast_node base;

token* tok;

ast_node* left;
ast_node* right;
} ast_node_binop;

现在,我有一个如下所示的方法:

void do_something(ast_node* node) {
...
}

我想传入 ast_node_binop 结构的实例...如果我将 ast_node_binop 转换为 ast_node ,则不会引发编译错误,但 valgrind 说:

==6554== Memcheck, a memory error detector
==6554== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==6554== Using Valgrind-3.12.0.SVN and LibVEX; rerun with -h for copyright info
==6554== Command: ./cola.out examples/hello_world.cola
==6554==
==6554== Stack overflow in thread #1: can't grow stack to 0xffe801000
==6554==
==6554== Process terminating with default action of signal 11 (SIGSEGV)
==6554== Access not within mapped region at address 0xFFE801FE8
==6554== Stack overflow in thread #1: can't grow stack to 0xffe801000
==6554== at 0x109B20: parse_expr (parse.c:88)
==6554== If you believe this happened as a result of a stack
==6554== overflow in your program's main thread (unlikely but
==6554== possible), you can try to increase the size of the
==6554== main thread stack using the --main-stacksize= flag.
==6554== The main thread stack size used in this run was 8388608.
==6554== Stack overflow in thread #1: can't grow stack to 0xffe801000
==6554==
==6554== Process terminating with default action of signal 11 (SIGSEGV)
==6554== Access not within mapped region at address 0xFFE801FD8
==6554== Stack overflow in thread #1: can't grow stack to 0xffe801000
==6554== at 0x4A266B0: _vgnU_freeres (vg_preloaded.c:59)
==6554== If you believe this happened as a result of a stack
==6554== overflow in your program's main thread (unlikely but
==6554== possible), you can try to increase the size of the
==6554== main thread stack using the --main-stacksize= flag.
==6554== The main thread stack size used in this run was 8388608.
==6554==
==6554== HEAP SUMMARY:
==6554== in use at exit: 4,587 bytes in 6 blocks
==6554== total heap usage: 16 allocs, 10 frees, 9,281 bytes allocated
==6554==
==6554== LEAK SUMMARY:
==6554== definitely lost: 0 bytes in 0 blocks
==6554== indirectly lost: 0 bytes in 0 blocks
==6554== possibly lost: 0 bytes in 0 blocks
==6554== still reachable: 4,587 bytes in 6 blocks
==6554== suppressed: 0 bytes in 0 blocks

我看了这个 stackoverflow 帖子:Struct Inheritance in C了解如何在 C 中实现“struct”继承。

我基本上只是希望能够将从另一个结构派生的结构实例传递到需要基/父结构的函数中。

正确的做法是什么?我将拥有来自 ast_node 的多个“派生”结构,并且我并不总是知道它是哪个派生结构,我只知道它们将始终派生自 ast_node 开头:结构中的 ast_node base;

我知道这令人困惑,但希望您能理解我想要实现的目标。

这里是 parse.c 因为评论中的人说 valgrind 提示了其他事情:https://pastebin.com/SvqeHKqs

最佳答案

这才是正确的做法。发布的错误与继承或类型转换没有任何关系,而是某种完全不相关的堆栈溢出。也许您正在使用递归或其他东西 - 我不知道。

ast_node_binop* 转换为 ast_node* ,然后让该函数访问 ast_node_binop 内的数据,这是完全正确且定义明确的。

从形式上来说,这是由 C17 6.5 §7 保证的,ast_node_binop 是一个聚合,在其成员中包含左值类型 ast_node。它是结构体的第一个成员,因此有关填充和对齐的问题也不适用。

关于C - 将 "composited/inherited"结构实例传递给其基本类型的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53276402/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com