memcpy with destination pointer to const data

前端 未结 3 581
我在风中等你
我在风中等你 2021-01-13 04:41

I always thought that an statement like const int *a means a is an int pointer to const data and as such one should not b

3条回答
  •  时光取名叫无心
    2021-01-13 05:15

    Casts usually suppress warnings. There is a gcc option, -Wcast-qual that will warn you about casts that are losing a const or volatile qualifier.

    The program ran successfully because the memory used to store the array wasn't actually readonly, because they were allocated on the stack. This is an implementation detail and technically your code could have crashed if the implementation was really strict.

    Declare a and b as globals and there's a greater chance it will crash (still not a guarantee)

提交回复
热议问题