Disable GCC “may be used uninitialized” on a particular variable

前端 未结 5 562
故里飘歌
故里飘歌 2021-02-01 03:09

I\'m getting this warning on a stack variable:

warning: object.member may be used uninitialized in this function

In this case I do not wish to

5条回答
  •  鱼传尺愫
    2021-02-01 03:45

    @Nawaz has answered the question as specifically asked, but have you considered that the fact that you need this may indicate you're declaring your struct too early/at a less nested scope than appropriate? It would generally be much preferred if you could declare your struct at a point where you can actually initialize it rather than declaring it earlier and filling it in various locations.

    Also, even though you can verify that it's never used uninitialized right now, what if someone else adds a new code path in the future and it's not initialized properly? If you disable the warning then it'll silently compile and probably break in an unexpected way. Unless you can prove that the initialization is taking a measurable amount of your program's CPU it's probably better to just do the initialization up front.

提交回复
热议问题