C初学题:指针运算>完成后清理
我正在慢慢掌握指针的窍门。但我还有一些疑问。
使用指针算术时是否可能导致内存泄漏,因为您正在移动指针指向的实际位置?
我的意思是,如果说我向上计数以逐个字符复制字符串,我是否需要向下计数以便 C“知道”指针曾经指向的位置?
谢谢 坦率
I'm slowly getting the hang of pointers. But there are still some questions I have.
Is it possible to cause memory leaks when using pointer arithmetic because you are shifting the actual position of where the pointer is pointing to?
I mean, if say I count upwards to copy a string char by char, would I need to count down so C "knows" where the pointer used to point to?
Thanks
Frank
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当使用
malloc()
或类似函数并且在需要时不调用free()
时,可能会发生内存泄漏。free()
应该总是使用malloc()
返回的指针来调用,所以如果你这样做的话是的:会导致未定义的行为。可能是内存泄漏,可能是分段错误,
一切皆有可能。
A memory leak is possible when using
malloc()
or similar functions and not callingfree()
when it's needed.free()
should always be called with the pointer returned bymalloc()
, so yes if you do something like this:causes undefined behaviour. Could be a memory leak, maybe a segmentation fault,
anything is possible.
内存是在堆上分配的。指针就是指向内存中位置的指针。您需要知道已分配内存的起始地址,以便稍后释放它。
这是因为内存管理系统需要记住有关已分配内存的信息(例如分配了多少内存),以便它知道稍后要释放多少内存,并防止它将同一块分配给另一个 malloc 调用。内存的起始地址是识别它的地址。
如果您想摆弄指针,请复制它并且不要修改原始指针。
Memory is allocated on the heap. A pointer is just that, a pointer to the location in memory. You need to know the address of the start of the allocated memory in order to free it later.
This is because the information about allocated memory (e.g. how much was allocated) needs to be remembered by the memory management system so it knows how much to free later, and to prevent it from allocating the same block to another malloc call. The start address of the memory is what identifies it.
If you want to fiddle with the pointer, take a copy of it and don't modify the original pointer.
动态内存分配会发生内存泄漏。如果您存储指向已分配的堆段的指针,然后修改该指针引用,那么您可能无法释放先前分配的内存。
您应该使用另一个指针,并保留对已分配内存的初始引用。
例如:
Memory leaks occurs with dynamic memory allocation. If you store a pointer to an heap segment that you have allocated and then modify that pointer reference, then maybe you will not be able to release the previous allocated memory.
You should use another pointer, and keep the initial reference to the allocated memory.
For example:
您可以使用指针算术创建内存泄漏,方法是将指针指向错误的位置,以便不再有任何对您所指向的内存块的引用。
无论指向的数据是使用 malloc() 还是静态分配,都会发生内存泄漏。然而,malloc() 的动态内存泄漏是危险的,而静态内存泄漏是无害的。
请注意,指向数组外部是未定义的行为:任何事情都可能发生。对指向不同数组的指针进行指针算术也是未定义的行为。
未定义行为的一些示例:
You can create memory leaks with pointer arithmetic, by having the pointer point at the wrong location, so that there are no longer any references to the chunk of memory you were pointing at.
It is a memory leak no matter if the data pointed at was allocated with malloc() or statically. Dynamic memory leaks with malloc() are however dangerous, while static memory leaks are harmless.
Note that pointing outside the array is undefined behaviour: anything can happen. Doing pointer arithmetics on pointers pointing at different arrays is also undefined behavior.
Some examples of undefined behavior: