Is it undefined behavior to form a pointer range from a stack address?

后端 未结 6 1564
走了就别回头了
走了就别回头了 2020-12-20 15:47

Some C or C++ programmers are surprised to find out that even storing an invalid pointer is undefined behavior. However, for heap or stack arrays, it\'s okay to store the a

6条回答
  •  萌比男神i
    2020-12-20 16:20

    In general it would be undefined behaviour to point beyond the memory space, however there is an exception for "one past the end", which is valid according to the standard.

    Therefore in the particular example, &c+1 is a valid pointer but cannot be safely dereferenced.

提交回复
热议问题