When you make a malloc call, in simplified terms the OS stores a record of the memory position that you have been given for use and how many bytes from it are available. So do something like free(&lista[2])
(address indicator was missing) does not work, because this address was not returned by a malloc call, only coincides to be in the 6 * 52 bytes range from the registered address.
Vectors are contiguous memory spaces, and not as flexible as lists. If you want to free up lista[2]
, The way to do it is like Anthony said: relocate and move. But keep in mind that this changes the position of the original indexes and begins to become costly in large vectors.
Alternatively, there is a simpler solution: to make a pointer vector for struct. So each position of it would take up to a maximum of 8 bytes (which would only allow you to release at the end of the program, if you’re not too worried about memory) and it would be okay to do something like free(lista[2])
(in this case, you would not be releasing the vector position, but rather the address pointed by the struct pointer in memory).
You will need to "reorder" the elements (bringing everything after
lista[2]
a down position and then userealloc
or use something likememmove
. A pointer structure is more flexible when we need to do this kind of "middle" removal of the placeholder.– Anthony Accioly
Here’s an example: http://answall.com/a/54608/101 It’s not the same because it’s a linked list, but it gives an idea. There is one in Java that is closer to what you need to do (not the best algorithm): http://answall.com/a/127008/101. If I find another, I put it here.
– Maniero
Bringing everything to a previous position is already beyond what I want, I need to at least free that memory space, with free(), but just put free(list[2]); it doesn’t work
– Eduardo Balestrin