-4

Although arrays are basically pointers, freeing char[] in C gives an error.

#include <stdlib.h>

int main(void) {
    char ptr[] = "Hello World";

    free(ptr); // this gives error at run time
}

ERROR: nexc(4212,0x10038e3c0) malloc: * error for object 0x7fff5fbff54c: pointer being freed was not allocated * set a breakpoint in malloc_error_break to debug

The interesting part is that, it is saying I am freeing a pointer which is not allocated.
How could this happen?

But in C++, compiler gives me a compile time error instead.

int main(void) {
    char ptr[] = "Hello World";

    delete ptr; // this gives error at compile time
}

like, Cannot delete expression of type char[12]

I thought this is because of compiler handles the char[12] by allocating when the function is called and deallocating the memory when the function ends. So, I write some codes after free(ptr); before the function ends.

#include <stdlib.h>

int main(void) {
    char ptr[] = "Hello World";

    free(ptr); // this still gives error at run time

    printf("\n");
    printf("\n");
    printf("\n");
    printf("\n");
    printf("\n");
}

This still gives error. How is this happening?

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
Cosmos Man
  • 593
  • 3
  • 15

6 Answers6

4

You only free what you have allocated using malloc (directly or indirectly) or related function (like realloc).

Attempting to pass a pointer not returned by malloc will lead to undefined behavior.

That you get a compiler error for delete in C++ is first and foremost because C and C++ are different languages with different rules.

And remember, an array is an array, not a pointer. Though an array can decay to a pointer to its first element in many situation (like when passing it to a function).

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
3

You only call free on dynamic memory that you've allocated using malloc, calloc &c.. Similarly, you only call delete on memory allocated with new. In your case, the C++ compiler is required to issue a diagnostic since pointer decay is not permitted to occur in this particular instance, and delete requires a pointer type.

The behaviour on attempting to call free on automatic memory is undefined.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

You only need to free what was malloced.
Your ptr is not a pointer, it is an array; an automatic local (inside main()) variable. It does not need freeing and attempting to free it is a mistake.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
0

All the static Strings will be allocated in the data section. You can't free data from this section. Threrefore, you can free only data the you have allocated with malloc (calloc / ralloc)

Kram
  • 526
  • 5
  • 11
  • "All the static Strings will be allocated in the data section.", please quote the standard that say this non sense. – Stargateur Jan 10 '18 at 09:02
  • Read about data segment and what it contains – Kram Jan 10 '18 at 09:36
  • Sorry, can't find any reference to "data segment" in C standard ;) – Stargateur Jan 10 '18 at 09:40
  • Well actually you are right.. It is not referenced in C standard. But, I tried to explain to this guy why the free is not working.... – Kram Jan 10 '18 at 09:55
  • The C standard does not specify where variables are allocated. Local variables will however end up in `.stack` and string literals in `.text`. Nothing in this code will end up in `.data`. Regardless, this answer is still strange, because the OP is trying to free a local variable allocated on the stack, not the string literal itself. – Lundin Jan 10 '18 at 10:20
0

in C programming language, you cannot write the instruction you made for obvious reasons. First of all, it must be understood that a pointer variable is a variable like so many others with a type except that it contains only addresses, so it is called a pointer variable because it contains the address of what is in memory. A pointer is therefore an address associated with a type of data and these two elements are inseparable.

If the pointer variable contains the address of an object of the whole type, the pointed object is used to know how to interpret the bits that make up this object, as well as its size. The instruction you wrote is therefore a constant pointer to a character type stored somewhere in memory. The string is therefore probably placed in a read-only data segment and therefore you cannot modify the string or release its memory, because the pointer variable does not point to a string in a space allocated dynamically beforehand by the "malloc" or "calloc" allocation function, and that is why you have the error message.

The second error is due to a confusion of your part here too, you must understand that there is a difference between an array of characters and a pointer. In simpler terms, a pointer variable is not an array and an array is not a pointer, but access to the element is done in the same way. For an array, the characters that make up the string and end with "\0" can be changed, but the array of characters will always point to the same address in memory and the pointer can contain another address, but beware, if you allocate the memory with a pointer and then point to other pars without releasing the memory you allocated creates a memory leak.

sambia39
  • 35
  • 9
-2

This is the proper way of using delete ! you first have to let the compiler know that the variable ptr is dynamic by using new!

#include<iostream>
#include<new>
using namespace std;

int main(void) {

    char *ptr;
    try{
    ptr = new char [20];
    } catch(bad_alloc xa){
        cout<<"error";
    }

    ptr= "Hello World";
    cout<<ptr;

    delete [] ptr; 
}
Dhruva-404
  • 168
  • 2
  • 10