i saw that we should not use the combination of new operator and free for allocating and deallocating memory in c++..
Is this right?
is there any reason other than saying that the combination is calloc/free and new/delete?
please help me..
thanks in advance

Recommended Answers

All 12 Replies

ok the only reason is it may cause problem when it is used in any other compiler or version

are the programs written in C++ are portable?

Yes they are fairly portable, but it really depends on how you write your code. First, I would start getting the right compiler which works on most of the platforms like gcc or Comeau, but I really don't want to dive deep into that, or tell you which compiler you should use. There are many criticism about different compilers, you should read a few of those.

ok the only reason is it may cause problem when it is used in any other compiler or version

There are two big reasons:

  1. The new operator calls constructors and the delete operator calls destructors. malloc() and free() only work with raw memory. So if you use malloc() to allocate objects with constructors the constructors will not be called; this will probably crash your program. Likewise if you use new to allocate objects with destructors and free() to deallocate them, the destructors won't be called; this will probably cause resource leaks.
  2. The data structure used to store and manage allocated memory might be different between malloc()/free() and new/delete. If you mix them, the pointer might be valid with one but not the other and your program will probably crash.

    For example, on a Windows compiler the heap might be created with HeapCreate() instead of GetProcessHeap() and malloc()/free() could use a different heap object than new/delete like this:

    #include <Windows.h>
    
    class memory_manager
    {
    public:
        memory_manager(): _heap(HeapCreate(0, 0, 0)) { }
        ~memory_manager() { HeapDestroy(_heap); }
    
        void *alloc(size_t bytes) { return HeapAlloc(_heap, 0, bytes); }
        void dealloc(void* p) { HeapFree(_heap, 0, p); }
    private:
        HANDLE _heap;
    };
    
    int main(void)
    {
        memory_manager new_manager;
        memory_manager malloc_manager;
    
        void* p = new_manager.alloc(1);
    
        malloc_manager.dealloc(p); // Boom!
    }

ok thanks for telling me that c++ files are portable

There are two big reasons:

  1. The new operator calls constructors and the delete operator calls destructors. malloc() and free() only work with raw memory. So if you use malloc() to allocate objects with constructors the constructors will not be called; this will probably crash your program. Likewise if you use new to allocate objects with destructors and free() to deallocate them, the destructors won't be called; this will probably cause resource leaks.
  2. The data structure used to store and manage allocated memory might be different between malloc()/free() and new/delete. If you mix them, the pointer might be valid with one but not the other and your program will probably crash.

    For example, on a Windows compiler the heap might be created with HeapCreate() instead of GetProcessHeap() and malloc()/free() could use a different heap object than new/delete like this:

    #include <Windows.h>
    
    class memory_manager
    {
    public:
        memory_manager(): _heap(HeapCreate(0, 0, 0)) { }
        ~memory_manager() { HeapDestroy(_heap); }
    
        void *alloc(size_t bytes) { return HeapAlloc(_heap, 0, bytes); }
        void dealloc(void* p) { HeapFree(_heap, 0, p); }
    private:
        HANDLE _heap;
    };
    
    int main(void)
    {
        memory_manager new_manager;
        memory_manager malloc_manager;
    
        void* p = new_manager.alloc(1);
    
        malloc_manager.dealloc(p); // Boom!
    }

the one u explained is a predefined one or just an example?

Sorry to ask this i am a beginner in C++ and really want to know about this

the one u explained is a predefined one or just an example?

I don't understand the question.

#include <Windows.h>

class memory_manager
{
public:
    memory_manager(): _heap(HeapCreate(0, 0, 0)) { }
    ~memory_manager() { HeapDestroy(_heap); }

    void *alloc(size_t bytes) { return HeapAlloc(_heap, 0, bytes); }
    void dealloc(void* p) { HeapFree(_heap, 0, p); }
private:
    HANDLE _heap;
};

int main(void)
{
    memory_manager new_manager;
    memory_manager malloc_manager;

    void* p = new_manager.alloc(1);

    malloc_manager.dealloc(p); // Boom!
}

Is this a predefined one ?
where did u use malloc or new in this code?

Is this a predefined one ?

I don't know what you mean by 'predefined', but that's just an example of how mixing two different heaps will blow up in your face. It's an empirical proof of this statement: 'The data structure used to store and manage allocated memory might be different between malloc()/free() and new/delete. If you mix them, the pointer might be valid with one but not the other and your program will probably crash.'

where did u use malloc or new in this code?

Nowhere, because they weren't relevant to the point of the code.

ok
so its not preferable to use the odd combinations..
thanks

so its not preferable to use the odd combinations..

...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.