C语言内存指针问题:同区指针释放与realloc后指针使用及释放疑问
C动态内存管理常见问题解答
Hey there! Let's walk through your two memory management questions—these are classic pitfalls in C, so it's smart to get them straight.
问题1:当两个指针指向同一块内存区域,其中一个被释放后会发生什么?
When you have two pointers pointing to the same dynamically allocated memory block, and you free() one of them, here's what goes down:
- The memory block gets returned to the operating system's heap pool. The pointer you called
free()on becomes a dangling pointer (most systems don't automatically set it toNULL). - The other pointer, which still holds the old memory address, is also a dangling pointer. Using it to read from or write to that memory is undefined behavior—this means anything could happen:
- You might read garbage data that's been overwritten by other processes or the system.
- Writing to that address could corrupt other parts of your program's memory, leading to crashes (like segmentation faults) or weird, hard-to-debug bugs that only show up randomly.
- Pro tip: After calling
free(), always set the pointer toNULLimmediately. That way, if you accidentally try to use it later, you'll get a clear crash (or can checkif (ptr != NULL)to avoid the issue entirely).
问题2:关于realloc的代码分析
Let's break down your code snippet step by step:
char *oldPointer, *newPointer; oldPointer = (char*)malloc(1000); newPointer = (char*)realloc(oldPointer, 2000);
此时还能使用oldPointer吗?会发生什么?
Short answer: No, you should never use oldPointer after calling realloc on it. Here's why:
reallocworks in two ways:- If there's enough free space right after the original block, it expands the block in place. Even though
oldPointerandnewPointerwould point to the same address here, the C standard explicitly states that the original pointer (oldPointer) becomes invalid oncereallocsucceeds. - If there isn't enough contiguous space,
reallocallocates a new 2000-byte block, copies the data from the old block into it, and automaticallyfree()s the old block. In this case,oldPointeris now pointing to memory that's already been returned to the system—total dangling pointer territory.
- If there's enough free space right after the original block, it expands the block in place. Even though
- Either way, using
oldPointerafterreallocis undefined behavior. You might get lucky and have it work once, but it's a ticking time bomb for crashes or corrupted data.
执行oldPointer = newPointer;后,若调用free(newPointer)会发生什么?
After you assign oldPointer = newPointer;, both pointers point to the same 2000-byte memory block. When you call free(newPointer):
- The memory block is released back to the heap. Now both
oldPointerandnewPointerare dangling pointers—they still hold the address of the freed block, but that memory is no longer yours to use. - If you try to read/write through either pointer afterward, you'll run into the same undefined behavior issues as before.
- One critical thing to avoid: Don't call
free(oldPointer)next! That would be a double free, which corrupts the heap and almost always causes a program crash.
内容的提问来源于stack exchange,提问作者Begench
相关产品推荐
相关产品推荐

