5

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

Joe
  • 46,419
  • 33
  • 155
  • 245
Frank Vilea
  • 8,323
  • 20
  • 65
  • 86
  • 2
    Yes (although it'll probably go *BANG*, rather than leak memory)... the pointer needs to point to the start of the block when you call free – forsvarir May 11 '11 at 12:56
  • 2
    It only matters if the pointer was the result of a `malloc` call. Normally if you need to walk through the dynamically-allocated block, you'd use a second pointer variable or use an array index and keep the original pointer variable unmodified. – John Bode May 11 '11 at 13:30

4 Answers4

9

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.

insumity
  • 5,311
  • 8
  • 36
  • 64
  • Thanks equality. When you close the program, would the memory leak remain - I mean is it gone until you restart the machine? Or does it depend on the OS you are using? – Frank Vilea May 11 '11 at 13:04
  • @Frank Vilea. Memory should be released by your OS when the process ended. So you don't need to restart your machine. Anyway it's always the OS that release the memory, so maybe exists some OS that don't do that (I don't know any of them). The problem of memory leaks is that your running application is consuming more memory than it needs. – Heisenbug May 11 '11 at 13:09
  • 1
    See this question for some interesting discussion http://stackoverflow.com/questions/2213627/when-you-exit-a-c-application-is-the-malloc-ed-memory-automatically-freed – Joe May 11 '11 at 13:10
  • Saying it "could cause a memory leak, but it will most probably cause a segmentation fault." is rather misleading. It causes *undefined behavior*. At this point talking about memory leaks is meaningless because you have a much worse problem. – R.. GitHub STOP HELPING ICE May 11 '11 at 13:17
8

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);
Joe
  • 46,419
  • 33
  • 155
  • 245
4

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);
Heisenbug
  • 38,762
  • 28
  • 132
  • 190
1

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 */
Lundin
  • 195,001
  • 40
  • 254
  • 396