簡體   English   中英

沒有動態內存分配的自引用(遞歸)結構

[英]Self-referencial (recursive) struct with no dynamic memory allocation

我有一個表示 AST 的結構,我在一個嵌入式設備上,我沒有 malloc,一切都應該在堆棧上或全局上。

所以我的結構;

/* AST Structre */
typedef struct ast {
    uint8_t type; /* This is the token value in grammar.h */

    /* Value of the token */
    union {
        double number;
        char literal;
    } value;

    struct ast *left; /* left hand side of the node */
    struct ast *right; /* right hand side of the node */
} ast_t;

我的問題是在沒有 malloc 的情況下使用遞歸結構的最佳方法是什么。

只需創建一個數組並根據需要進行初始化/分配。

ast_t fred[] = { { 1, {2.0}, NULL,     &fred[1]}, 
                 { 3, {4.0}, &fred[0], NULL    } };

調用ast_t函數foo(fred); . 一定不要打電話給free(fred)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM