开发者

C Beginner question: Pointer arithmetic > cleaning up once you are done

开发者 https://www.devze.com 2023-03-04 22:15 出处:网络
I\'m slowly getting the hang of pointers. 开发者_StackOverflow中文版But there are still some questions I have.

I'm slowly getting the hang of pointers. 开发者_StackOverflow中文版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


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.


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);


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);


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 */
0

精彩评论

暂无评论...
验证码 换一张
取 消