C初学题:指针运算>完成后清理

发布于 2024-11-06 10:38:09 字数 148 浏览 1 评论 0原文

我正在慢慢掌握指针的窍门。但我还有一些疑问。

使用指针算术时是否可能导致内存泄漏,因为您正在移动指针指向的实际位置?

我的意思是,如果说我向上计数以逐个字符复制字符串,我是否需要向下计数以便 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

祁梦 2024-11-13 10:38:09

当使用 malloc() 或类似函数并且在需要时不调用 free() 时,可能会发生内存泄漏。 free() 应该总是使用 malloc() 返回的指针来调用,所以如果你这样做的话是的:

int* ptr = malloc(sizeof(int));
free(ptr + 1);

会导致未定义的行为。可能是内存泄漏,可能是分段错误,
一切皆有可能。

A memory leak is possible when using malloc() or similar functions and not calling free() when it's needed. free() should always be called with the pointer returned by malloc(), so yes if you do something like this:

int* ptr = malloc(sizeof(int));
free(ptr + 1);

causes undefined behaviour. Could be a memory leak, maybe a segmentation fault,
anything is possible.

平定天下 2024-11-13 10:38:09

内存是在堆上分配的。指针就是指向内存中位置的指针。您需要知道已分配内存的起始地址,以便稍后释放它。

这是因为内存管理系统需要记住有关已分配内存的信息(例如分配了多少内存),以便它知道稍后要释放多少内存,并防止它将同一块分配给另一个 malloc 调用。内存的起始地址是识别它的地址。

如果您想摆弄指针,请复制它并且不要修改原始指针。

int *x = malloc(...);
int *y = x;

... pointer arithmetic with y

free(x);

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.

int *x = malloc(...);
int *y = x;

... pointer arithmetic with y

free(x);
痴骨ら 2024-11-13 10:38:09

动态内存分配会发生内存泄漏。如果您存储指向已分配的堆段的指针,然后修改该指针引用,那么您可能无法释放先前分配的内存。

您应该使用另一个指针,并保留对已分配内存的初始引用。
例如:

 char *pointer = (char*)malloc (SIZE);    /*alloc space for storing a string of size SIZE*/
 char *pointer2 = pointer;
 int i;
 for (i = 0 ; i < SIZE ; i++){
      pointer2 += 1;
      //you are modifying the second pointer so you always keep a reference to the allocated memory(pointer)
 }

 //now you can call free on your memory
 free(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:

 char *pointer = (char*)malloc (SIZE);    /*alloc space for storing a string of size SIZE*/
 char *pointer2 = pointer;
 int i;
 for (i = 0 ; i < SIZE ; i++){
      pointer2 += 1;
      //you are modifying the second pointer so you always keep a reference to the allocated memory(pointer)
 }

 //now you can call free on your memory
 free(pointer);
偏爱你一生 2024-11-13 10:38:09

您可以使用指针算术创建内存泄漏,方法是将指针指向错误的位置,以便不再有任何对您所指向的内存块的引用。

无论指向的数据是使用 malloc() 还是静态分配,都会发生内存泄漏。然而,malloc() 的动态内存泄漏是危险的,而静态内存泄漏是无害的。

请注意,指向数组外部是未定义的行为:任何事情都可能发生。对指向不同数组的指针进行指针算术也是未定义的行为。

未定义行为的一些示例:

typedef struct
{
  char array1 [6] = "hello";
  char array2 [6] = "world";
} HelloWorld_t;


HelloWorld_t hw;
const char* ptr = hw.array1;
ptr += 6; /* undefined behavior, out of bounds of the original array */
puts(ptr); /* anything can happen here: the program may crash */
puts(array2 - 6); /* also undefined behavior */

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:

typedef struct
{
  char array1 [6] = "hello";
  char array2 [6] = "world";
} HelloWorld_t;


HelloWorld_t hw;
const char* ptr = hw.array1;
ptr += 6; /* undefined behavior, out of bounds of the original array */
puts(ptr); /* anything can happen here: the program may crash */
puts(array2 - 6); /* also undefined behavior */
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文