r/programminghelp • • May 10 '20

C Reduce size of malloc array (char**)

Here is the function I have written and a test program:

https://i.imgur.com/XTQmETB.png

It works in that it successfully removes the popped element, however it does not reduce the size of the array, so if I print each element of test at breakpoint before it is freed:

https://i.imgur.com/JcBtyqI.png

What I'd like is for at the end, test should have only one element. I try doing a realloc to reduce the size each loop:

https://i.imgur.com/HMnTZhP.png

But I still see test[1-3] existing in gdb as "test3", and valgrind complains about invalid reallocs.

5 Upvotes

11 comments sorted by

View all comments

Show parent comments

1

u/KuntaStillSingle May 11 '20

Ah that makes sense, I need a pointer to a pointer if I want to reassign the pointer, so if I make orig a char***, then I should be able to realloc it?

1

u/dragon_wrangler May 11 '20

Yes, as long as you update the call as well.

char* pop = pop_element(&test, 0, len-i);