Is it bad practice to declare an array mid-function

后端 未结 5 1141
伪装坚强ぢ
伪装坚强ぢ 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

    Generally yes, this is bad practice, although new standards allow you to use this syntax. In my opinion you must allocate (on the heap) the memory you want to use and release it once you're done with it. Since there is no portable way of checking if the stack is enough to hold that array you should use some methods that can really be checked - like malloc/calloc & free. In the embedded world stack size can be an issue.

    If you are worried about fragmentation you can create your own memory allocator, but this is a totally different story.

提交回复
热议问题