Using original pointer after realloc?

后端 未结 2 1670
后悔当初
后悔当初 2021-01-17 14:45

I was reading Richard Reese\'s new (May 2013) O\'Reilly book \"Understanding and Using C Pointers\", and I have a question about some code therein, on page 87.



        
相关标签:
2条回答
  • 2021-01-17 15:29

    It is safe to use the dangling pointer (e.g. for "pointer arithmetic") as long as you don't try to dereference the pointer (i.e. apply the operator *).

    0 讨论(0)
  • 2021-01-17 15:34

    once a pointer is dangling, is it safe to use the address contained in the pointer for any purpose, such as calculating offsets?

    No, it is not safe. After free the pointer value is an invalid address and an invalid address cannot be used for pointer arithmetic without invoking undefined behavior.

    0 讨论(0)
提交回复
热议问题