Is it bad practice to use multi-dimensional arrays in C/C++?

前端 未结 9 1063
北海茫月
北海茫月 2021-02-07 04:46

Some programmers seem to violently hate them, while others seem to think they\'re fine. I know that anything that can be done to a multi-dimensional array can also be done to a

9条回答
  •  耶瑟儿~
    2021-02-07 05:21

    Advantages of multi-dim arrays to Vector>

    1. Easy to type [ ][ ]
    2. C compatible.
    3. Simple to understand conceptually what is it is doing.

    Disadvantages:

    1. No easily detected bounds checking. Bounding off the end of the outer brackets usually overflows into memory allocated by the inner brackets making these types of error a real pain to track.
    2. Jagged arrays require care to set up. Vector pattern is easy.
    3. Multidimensioal arrays are more than double pointers making it a pain to pass into functions correctly. Most of the time, I've seen them just passed as a raw address to a double pointer which defeats the intrinsic math the compiler will do for you.

    Basically though, it comes down to lack of bounds checking for me.

提交回复
热议问题