Is it bad practice to declare an array mid-function

后端 未结 5 1171
伪装坚强ぢ
伪装坚强ぢ 2021-01-15 00:06

in an effort to only ask what I\'m really looking for here... I\'m really only concerned if it\'s considered bad practice or not to declare an array like below where the siz

5条回答
  •  没有蜡笔的小新
    2021-01-15 00:52

    That depends. The first clearly isn't what I'd call "proper", and the second is only under rather limited circumstances.

    In the first, you shouldn't cast the return from malloc in C -- doing so can cover up the bug of accidentally omitting inclusion of the correct header ().

    In the second, you're restricting the code to C99 or a gcc extension. As long as you're aware of that, and it works for your purposes, it's all right, but hardly what I'd call an ideal of portability.

    As far as what you're really asking: with the minor bug mentioned above fixed, the first is portable, but may be slower than you'd like. If the second is portable enough for your purposes, it'll normally be faster.

提交回复
热议问题