Why do compilers not warn about out-of-bounds static array indices?

前端 未结 10 1780
心在旅途
心在旅途 2020-12-03 03:07

A colleague of mine recently got bitten badly by writing out of bounds to a static array on the stack (he added an element to it without increasing the array size). Shouldn

10条回答
  •  臣服心动
    2020-12-03 03:54

    shouldn't the compiler emit a warning at the very least?

    No; C compilers generally do not preform array bounds checks. The obvious negative effect of this is, as you mention, an error with undefined behavior, which can be very difficult to find.

    The positive side of this is a possible small performance advantage in certain cases.

提交回复
热议问题