On what architectures is calculating invalid pointers unsafe?

前端 未结 6 1357
小蘑菇
小蘑菇 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:50

    Either way, a well-defined, zero-overhead way of creating a one-based array is the following:

    int* a = new int[6];
    

    There, problem solved. ;-) (But interesting question still.)

提交回复
热议问题