Free() in struct with char array

Asked

Viewed 427 times

0

I have the struct

struct data {
    int code;
    char name[50];
    char feature[30];
};
typedef struct data Data;

and the function:

void free_data(Data *data) {
    if (data != NULL)
        free(data);
}

But the free() is not rendering the pontei null, that is, when making

data == NULL; // returns false

I’ve tried many other ways, but I haven’t been successful.

From now on, thank you.

  • How do you say free is not working?

  • @Jeffersonquesado, my goal is that the free make the pointer null. I will edit in the question, because I think I did not express myself well.

  • 1

    But the free does not do this. Not to mention that as you pass the pointer as value (not as reference), everything you do in the variable data will not affect the parameter.

  • 1

    Where did you use the malloc()? Why do you want it to be null?

  • 1

    Reinforcing what @Jeffersonquesado said, free does not render the pointer null, only releases the associated space, something that can confirm in the free documentation. As a last note, not even the space released is placed at NULL or 0, is only marked as free internally

2 answers

5


You can use a macro if you are careful. The use of macros is subject to criticism, not to mention that using carelessly will produce wrong results.

Look that question on Soen to understand how poorly defined macros can disrupt the life of the programmer

What you want is to have the following text:

if (ptr != NULL) {
  free(ptr);
  ptr = NULL;
}

To put in a macro you could define it like this:

#define libera_anula(ptr) do { if ((ptr) != NULL) {\
    free(ptr);\
    (ptr) = NULL;\
  }\
} while(0)

Note that the do..while was put in such a way that you are bound to put the ; to call this macro, making it look like a language function that has the side effect of nullifying the value of the variable passed as parameter. See more: Why use while(0)?

It is also worth noticing that calls from ptr i placed in parentheses to try to mitigate some problems of pointer arithmetic.

And as stated in another answer and in the comment from @Maniero, you should only use free in a memory region that was dynamically allocated.

More about macros:

Red herring

A red herring is a strategy to divert the focus of attention from what is actually happening to something of minimal significance. Like Aringarosa ("pink herring" in Italian, an oversight of the author who mistook it for the Spanish "rose" in Dan Brown’s "The Da Vinci Code". Your question is full of herring.

The first is in the title itself: "Free() in struct with char array". Here the character vector did not influence anything, nor will it occasionally influence its structure; it could include if it were dynamic vector allocation, but that is not the case, it is already allocated to the structure, it belongs to the structure.

Another red herring is that the free at no point was it indicated that it did not work, only that it "did not behave the way you wanted it to behave". For starters, no matter how much free is overloaded into a macro to do as is above in my answer, the simple fact that you pass a pointer to it to be released implies that you are not tampering with the original data, so even the macro would not solve your problem. When you pass a pointer as argument, this pointer is copied (pass by value) to be used internally in the function. Changing the value of the pointer only has effect inside the function, not outside. You can do data = NULL; within the function free_data that this will not have any external side effect to the function.

Anyway, a red herring you were a victim of was that at some point you connected the unallocated space with NULL, but that is not the case. The @Isac you understood that very well in his comment, even putting the official documentation of the function to you get more grounded.

2

You should use the free only in a dynamically allocated structure (with malloc/calloc). That is, the correct syntax would be:

typedef struct data {
  int code;
  char name[50];
  char feature[30];
}Data;

void free_data(Data *data) {
  if (data != NULL)
    free(data);
}

int main(){
  Data *data = malloc(sizeof(Data));
  ...
  Outras instruções utilizando 'data'
  ...
  free_data(data);
  return 0;
}

Browser other questions tagged

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