What if, memory allocated using malloc is deleted using delete rather than free

前端 未结 7 1651
渐次进展
渐次进展 2021-02-09 03:45

I came across an issue which I could not resolve.

My question is, if I used malloc to allocate memory and then memory block is delete using delete

相关标签:
7条回答
  • 2021-02-09 03:58

    The free function expects a pointer to memory allocated by malloc. The compiler is simply telling you that did not pass it a pointer, a fact that it can readily prove. I guess you meant to create an object with new and then call free on the address of that object. But you did not call new.

    When you correct this you may well find the free succeeds but you should not read anything significant into any such success. You know the rules, they are clearly stated in your documentation. It is incorrect to do what you are attempting. The behaviour is undefined. There's little to be gained from an experiment like this. You won't learn anything about why the C memory management functions cannot be paired with C++ memory management functions.

    The problem with your experiment is that is has no power. You can just show the outcome for one specific scenario. But these functions are designed to work across the board. How are you going to test every single possible scenario?

    Never program using trial and error. You must always understand the rules and priciples behind what you do.

    0 讨论(0)
  • 2021-02-09 03:59

    Your b variable is not a pointer, but a stack-allocated A. If you want to try this experiment, then you'd want to say

    A* b = new A;
    free(b);
    

    I'm all for experimentation, but I'm not sure what you were trying to achieve here.

    In C++, operator new doesn't just allocate memory, it runs constructors too. Similarly, delete runs destructors for you (taking polymorphic classes into account), as well as freeing the returned memory. On the other hand, malloc() and free() are just C library routines which (from a programmer's point of view) do nothing more than request a chunk of memory from the operating system, and then hand it back.

    Now, for a simple, POD class with trivial constructors and destructors, you might be able to get away with mixing up C and C++ memory allocation, because there's nothing C++-specific to be done. But even if you do, what does it prove? It's undefined behaviour, and it probably won't work on another compiler. It might not work tomorrow.

    If you use any C++ features -- inheritance, virtual functions, etc etc, it very definitely won't work, because new and delete have to do a lot more work than just allocating memory.

    So please, stick to the rules! :-)

    0 讨论(0)
  • 2021-02-09 04:05

    You didn't create b using new so you are not testing the intended hypothesis. (Also, the name of b is not a pointer.)

    Anyway, malloc/new and delete/free work interchangeably only as long as their implementations are interchangeable. In simple cases and on simpler platforms this is more likely, but it's a risky bet in the long term or as a habit.

    For example, the array form of new, when applied to a class with a nontrivial destructor, requires the implementation (compiler, runtime, etc.) to remember the size of the array so that delete may call the destructor for each array object. There is almost nowhere to put such information but in the dynamically allocated block itself, preceding the memory location referred to by the pointer returned by new. Passing this pointer to free would then require the memory allocator to determine the actual start of the allocation block, which it may or may not be able to do.

    More generally, new and malloc may refer to completely different memory allocators. The compiler, runtime library, and OS work together to provide memory to your program. The details can change as a result of switching or upgrading any of these components.

    0 讨论(0)
  • 2021-02-09 04:11

    No, it is not a rule of thumb that you should not mix new/free or malloc/delete.

    Sure, it might be possible to use free to relinquish an allocation you obtained with new without a crash, but it is highly implementation/system specific.

    Ignoring the fact that you're bypassing destructors or writing code that will corrupt memory on some systems, the more fundamental, more basic C++ consideration is really simply this:

    malloc/free are global, system functions, while new and delete are operators.

    struct X {
        void* operator new(size_t);
        void operator delete(void* object);
    };
    

    Sure - you can borrow a book from Amazon and return it to your local library: go there late one night and leave it on their front door step.

    It's going to have undefined behavior, up to and including your getting arrested - and I'm talking about mixing new/free again here :)

    Put another way: malloc and free deal with raw memory allocations, while new and delete deal with objects, and that's a very different thing.

    void* p = new std::vector<int>(100);
    void* q = malloc(sizeof(*q));
    
    0 讨论(0)
  • 2021-02-09 04:12

    When you call delete a pointer, the compiler will call the dtor of the class for you automatically, but free won't. (Also new will call ctor of the class, malloc won't.)

    In you example, a char array apparently don't have a dtor, so delete does nothing but return the memory. That's why it's okay. (Or vise versa, new a char array and then free it.)

    But there can still be a problem: what if new and malloc are borrowing memory from different heap manager?

    Imagine that, you borrow money from people A, and return to people B later. Even if you are okay with that, have you ever consider A's feeling?

    BTW, you should use delete [] pArray; to free an array allocated using new[].

    0 讨论(0)
  • 2021-02-09 04:16

    From the documentation,

    void free (void* ptr); 
    

    ptr::

    Pointer to a memory block previously allocated with malloc, calloc or realloc.

    And what you are passing is not a pointer, so it complains.

     A *b = new A();
     cout << b->lol() << endl;
     free(b); // Will Not cause error
    
    0 讨论(0)
提交回复
热议问题