On what architectures is calculating invalid pointers unsafe?

前端 未结 6 1341
小蘑菇
小蘑菇 2020-12-16 21:05
int* a = new int[5] - 1;

This line by itself invokes undefined behavior according to the C++ standard because a is an invalid pointer and not one-p

6条回答
  •  情歌与酒
    2020-12-16 21:54

    I think this could possibly be unsafe on some old 16 bit x86 systems. The address is "split" between an address register and a segment register and I would guess that this could result in an invalid value being loaded into a segment register which would cause an exception.

    Probably not an issue as it's not a common architecture these days.

提交回复
热议问题