So, I have 2 structs.
The first one:
typedef struct AST_NODE_STRUCT {
token* tok;
} ast_node;
The other one:
typedef struct AST_BINOP_STRUCT {
ast_node base;
token* tok;
ast_node* left;
ast_node* right;
} ast_node_binop;
Now, I have a method that looks something like this:
void do_something(ast_node* node) {
...
}
And I want to pass in an instance of the ast_node_binop
struct...
No compilation errors are thrown if I cast ast_node_binop
to a ast_node
but valgrind says:
==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
I looked at this stackoverflow post: Struct Inheritance in C to see how to implement "struct" inheritance in C.
I basically just want to be able to pass in struct instances that are derived from another struct into functions that expects the base/parent struct.
What is the correct way to do this?
I will have multiple "derived" structs from ast_node
, and I will not always know which derived struct it is, I just know that they will always be derived from ast_node
Starting with: ast_node base;
in the struct.
I know its confusing but hopefully you will understand what I am trying to achieve.
Here is parse.c
since people in the comments said that valgrind complained about something else: https://pastebin.com/SvqeHKqs