Free () in struct with char array

0

I have 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 free () is not making the bridge null, ie when doing

data == NULL; // returns false

I have tried in many other ways, but I did not succeed.

Thanks in advance.

    
asked by anonymous 22.10.2017 / 00:40

2 answers

4

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

  

Look at this question in SOen to understand how ill-defined macros can disrupt the programmer's life.

What you want is to have the following text:

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

To by 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 do..while has been put in such a way that you are obliged to ; to call this macro, making it look like a language function that has the side effect of overriding the value of the variable passed as parameter. See more: Why do you use while (0)?

It's also worth noticing that ptr calls I placed in parentheses to try to mitigate some pointer arithmetic problems.

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

More about macros:

Red herring

A red herring is a strategy to divert attention from what is really happening to something of minimal significance. Like the Aringarosa ("pink herring" in Italian, an oversight of the author who confused it with the Spanish "rose") in Dan Brown's "The Da Vinci Code." Your question is full of herrings.

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

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

Anyway, a red herring that you were victim of was that at some point you related unallocated space with NULL , but that's not the case. @Isac got it right #

In your comment , even putting the official documentation of the function to get you more grounded.

    
22.10.2017 / 01:48
2

You should use 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;
}
    
22.10.2017 / 01:02