Why does gcc return 0 instead of the address of a stack allocated variable?

前端 未结 1 1309
孤独总比滥情好
孤独总比滥情好 2021-01-18 18:44

As part of an experiment using custom stacks, I wanted a function to return the address of a stack allocated char buffer.

// return pointer to stack variabl         


        
1条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-18 19:31

    GCC deliberately returns NULL in this case as can be seen from the code:

    tree zero = build_zero_cst (TREE_TYPE (val));
    gimple_return_set_retval (return_stmt, zero);
    update_stmt (stmt);
    

    Newer versions of GCC and Clang exploit undefined behavior more aggressively so this check is not suprising. It also makes code fail fast which is a good thing in most cases (not yours, apparently).

    0 讨论(0)
提交回复
热议问题