Use free() without malloc()?

Asked

Viewed 408 times

4

May cause some problem in using the free() on a pointer that was not allocated with malloc() beyond the necessity itself?

void funcao (int par)
{

    char  *palavra;

    if(par%2 == 0)
    {
         palavra = malloc(3*sizeof(char));
         strcpy(palavra, "par");
    }

    free(palavra);  
    palavra = NULL;  
}

this is just an example, your sense or implementation does not matter.

  • Give an example of such use.

  • There’s no point, just to give an example of what I meant

3 answers

6


In the shown example can cause trouble. It’s called dangling Pointer.

The free() is acting on the variable palavra that has not been initialized. That is, in C has a value in it, you have no idea what that value is, it will vary in each execution of the generated application. He’ll try to release the memory pointed at that address. If you’re lucky you can just fail and nothing happens or you can release something that was allocated and shouldn’t. Something disastrous can happen and even displace things completely outside of what is expected, since even the size of the allocation can be considered erroneously by free(). The information is virtually random.

In C we have to be very careful. One of these precautions is always initialize the variable. This would cause the free() wrong did not cause problems, although the code would not be strictly correct. The ideal is not to leave a free() free so (did not resist :) ). Will one day someone change something and it happens to be a problem even in cases where it was not initially.

  • was very enlightening, I confess that I have some doubts about memory allocation and addressing in C, which differs from other languages that I do not need to worry about it, but nevertheless it becomes well didactic, thank you

6

Here are some observations:

1 - It is good practice to initialize the pointers to NULL when declaring them, a pointer without proper initialization can point to an invalid memory address (dangling Pointer or pointer wild);

2 - By default, when free() receives a NULL pointer, no action is taken, ie there is no problem in passing a NULL pointer to free();

3 - When you allocate memory to accommodate strings, be sure to include a byte for the ' 0 terminator'.

Follow a suggested improvement to your code:

void funcao( int par )
{
    char * palavra = NULL;

    if( par % 2 == 0 )
    {
        palavra = malloc( (3 + 1) * sizeof(char) );
        strcpy( palavra, "par" );
    }

    free(palavra);  
}

I hope I’ve helped!

2

Lucas may cause several problems, because when the free function is to release the memory area that in theory should have been allocated with the malloc function and associated with a pointer, it will not be pointing to an area controlled by the compiler, in this way, the results may be unexpected. Take care and good luck.

  • But it was not allocated with the malloc(). Compiler does not control memory areas.

  • Only swap compiler for "C Runtime code used by compiler" . The answer makes sense yes.

Browser other questions tagged

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