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



所以,我有两个结构。

第一个:

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文章:C中的结构继承,看看如何在C中实现"结构"继承。

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

正确的方法是什么?我会有多个来自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的集合。它是结构的第一个成员,因此关于填充和对齐的问题也不适用。