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
These statements are not using malloc on newPtr.
The statement newPtr = malloc(10 * sizeof(int)); cause these operations:
sizeof(int) is evaluated.malloc is called, and that product is passed to it.malloc returns a value.newPtr.So you see, at step 3, newPtr is not involved in any way. Only after malloc is done is newPtr involved.
When you call malloc a second time, it has no way of knowing you are doing anything with newPtr. It merely allocates new space and returns a pointer to it. Then that new pointer is assigned to newPtr, which erases the old value that was in newPtr.
At that point, you have no way of knowing what the old value was. The space is still allocated, because it was not freed, but you do not have a pointer to it.