Delete a space allocated by malloc

Asked

Viewed 724 times

5

First I allotted 6 spaces of 52 bytes belonging to a struct in memory:

lista = malloc(6 * sizeof(registro));

In practice to access them via pointer is done:

lista[0], lista[1], lista[2]...

And how I would delete content saved in space lista[2]? Leaving the other 5 spaces intact?

  • 2

    You will need to "reorder" the elements (bringing everything after lista[2] a down position and then use realloc or use something like memmove. A pointer structure is more flexible when we need to do this kind of "middle" removal of the placeholder.

  • 2

    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.

  • 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

2 answers

2

You can use the function memmove() of the standard library string.h combined with the function realloc() of the standard library stdlib.h, Just look at the solution to your case:

registro * lista = NULL;

/* Aloca vetor de registro com 6 elementos */
lista = (registro*) malloc( 6 * sizeof(registro) );

/* Desloca os 3 ultimos elementos do vetor */
memmove( &lista[2], &lista[3], sizeof(registro) * 3 );

/* Realoca a memoria sem alterar os dados do vetor */
lista = (registro*) realloc( lista, 5 * sizeof(registro_t) );

...

/* libera a memoria alocada */
free(lista);
  • You have no comment on memmove which states that if memory regions overlap, the behaviour is unexpected?

  • There’s an observation that there may be overlap that the behavior will be done properly, actually. I must have confused it with another function, possibly another language

0

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

  • But "list", is already a pointer to structure. Being the name of my structure: "record", I declare the same: registro *lista;.

  • I don’t know if I understand it very well... list is a pointer to structure, but you’re accessing it like a vector?

  • So, list is a pointer to the structure, so I make a dynamic allocation using list. As mentioned above lista = malloc(6 * sizeof(registro));, I’m allocating 6 spaces, each with the size of the structure, and to access each I use: lista[0], lista[1]... lista[5].

  • Got it. But in this case, each space is a value, not a pointer. The solution I suggested would involve declaring list as pointer registo **lista, do the malloc for the vector and, for each position (which now holds a pointer), you make a malloc for the struct. This would allow you to give free(lista[i]) when such a position would become unnecessary to you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.