What happens if I use malloc twice on the same pointer (C)?

后端 未结 4 675
梦如初夏
梦如初夏 2020-12-30 01:31

Say for instance I created a pointer newPtr and I use malloc(some size) and then later I use malloc(some size) again with the same pointer. What happens? Am i then creating

4条回答
  •  独厮守ぢ
    2020-12-30 01:55

    Your program will have a memory leak. The first value of newPtr will be lost and you will not be able to free it.

    Am i then creating a second block of memory the same size of the first one?

    Yes. You are allocating a second object, distinct from the first one.

    Does newPtr point to the same address?

    No. The objects are distinct, so their address are distinct.

提交回复
热议问题