Checking if a variable is initialized

前端 未结 13 1445
后悔当初
后悔当初 2020-12-28 11:59

Seems like this would be a duplicate, but maybe it is just so obvious it hasn\'t been asked...

Is this the proper way of checking if a variable (not pointer) is init

13条回答
  •  感情败类
    2020-12-28 12:28

    You could reference the variable in an assertion and then build with -fsanitize=address:

    void foo (int32_t& i) {
        // Assertion will trigger address sanitizer if not initialized:
        assert(static_cast(i) != INT64_MAX);
    }
    

    This will cause the program to reliably crash with a stack trace (as opposed to undefined behavior).

提交回复
热议问题