2

I have a problem in a C program: I would like to free the first element from a dynamic array but I don't know if it is possible nor how I could do it. For instance if I allocate an array A made of 10 integers like this:

int *A;
A=(int*)malloc(sizeof(int)*10);

is it possible to free the first position of the array? In which way? I must optimize the program so I can't do some memcpy ignoring the first element or similar stuff because it would be a slowdown.

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
g.delv
  • 23
  • 3
  • you want to free the memory of the first array element? – Martín Zaragoza Jul 24 '18 at 18:19
  • No, it is not possible. – Eugene Sh. Jul 24 '18 at 18:21
  • You cannot free just part of an allocated block. Don't worry about micro optimizations- just skip the first element the simple way. – Dillon Davis Jul 24 '18 at 18:21
  • 1
    What is your goal exactly? – Leon Trotsky Jul 24 '18 at 18:22
  • I built a queue and it is built on a dinamic array because I don't know how much is the maximum of the elements in the queue. So when I reach the size limit I use 'realloc' function making it bigger. When I dequeue an element I would like to free that part of the memory because probably there will be a lot of memory lost instead. – g.delv Jul 24 '18 at 18:28
  • 1
    Then your design is not good enough. Either use a ring buffer or a linked list. – Eugene Sh. Jul 24 '18 at 18:33
  • You are going about it the wrong way. Do a little more research on how queues can be implemented and how you can re-use the space at the beginning of the array by wrapping around. – Christian Gibbons Jul 24 '18 at 18:34
  • @ChristianGibbons I can't use the space at the beginning becuase when the queue is full I reallocate memory beacause I don't know how much space I need – g.delv Jul 24 '18 at 19:02
  • @EugeneSh. What do you mean with ring buffer? – g.delv Jul 24 '18 at 19:03
  • @g.delv https://stackoverflow.com/questions/827691/how-do-you-implement-a-circular-buffer-in-c – Barmar Jul 24 '18 at 19:03
  • Your question is about freeing elements at the beginning. Instead of freeing them, you should be recycling them. Do some research on ring buffers as Eugene Sh. mentioned; that should get you started. – Christian Gibbons Jul 24 '18 at 19:04
  • Ok thank you very much guys – g.delv Jul 24 '18 at 19:06

2 Answers2

3

You cannot "free" a single portion of an allocated memory block. If you want to "ignore" the first element, you could use A+1 which points to the second element.

jaudo
  • 2,022
  • 4
  • 28
  • 48
2

When you allocate a chuck of memory, you can't deallocate a part of it. It has to be the whole chunk. More specifically, you can only pass to free exactly what was returned by malloc / calloc / realloc.

What you can do is use realloc to resize a memory region, however this is not something you want to do too frequently as it can affect performance. A good rule is to double the size when you want to expand and half the size when you want to contract.

dbush
  • 205,898
  • 23
  • 218
  • 273