CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2007
    Location
    Pune, INDIA
    Posts
    128

    Question Issue regarding new and free in C++

    Hi All,
    I am having a confusion in my mind (it is more or less conceptual) that if I am allocating a memory to any object by using "new". and later deallocating that by using "free" what problem will I be facing? Or will it be running smoothly?

    Please help me regarding to this problem.

    Thanks.
    Last edited by mayank_3103; October 13th, 2010 at 10:40 PM.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: Issue regarding malloc and free in C++

    The rule is very simple:
    all that was newed must be deleted and
    all what was mallocked must be freed.
    The result of any mixture of those - the undefined behavior and a lot of various problems (it might be "access violation" as well as many other errors, and some of them may appear in some other places than the problem lies)
    Victor Nijegorodov

  3. #3
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Issue regarding malloc and free in C++

    The problem you will be facing is:
    - Objects that are deallocated but not destroyed: free does not destroy objects (bad).
    - Undefined behaviour: It is illegal to free a new, or delete a malloc (extremely bad).

    Under no circumstances is it OK to free a new. You should not even "try to see if it runs".
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  4. #4
    Join Date
    Jul 2007
    Location
    Pune, INDIA
    Posts
    128

    Re: Issue regarding malloc and free in C++

    Thanks for the reply and I already applied in my demo code, and it worked. Though It might not destroyed the object, but It didnt shown me any error.

    So there must be any specific reason to avoid this type of programming, or its just the rule to use delete with new and free with malloc?

    Somewhere I read not to mix C and C++ concepts together as malloc is in C and new is given by C++, but I dont think this is the main reason.

    Any help will be greatly appreciated. This doubt is not yet cleared with me.

    Thanks.

  5. #5
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Issue regarding malloc and free in C++

    malloc simply allocates from the heap. new is a C++ keyword, that calls "operator new", which in turn can be overloaded to do weird and wonderful things behind the scene automatically: stack allocation; Pool allocation; logging.. You name it!

    Thus malloc/free are not compatible with new/delete.

    ...

    This is a simple explanation, but you should not rely on it. In particular, don't think you can delete a new just because your new is not overloaded. The bottom line is that mixing them has been declared ILLEGAL by the standard, and doing it creates undefined behaviour. That means you should not try to understand, nor test it. You don't do it, period.

    The thing about undefined behaviour is that the behaviour is undefined. You can test it as much as you want, and your results will be irrelevant.

    Just because it (seems to) works doesn't mean it is right. That is both the beauty, and the bane, of C++.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  6. #6
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Issue regarding malloc and free in C++

    I'm also fairly certain that new will word align your objects, so it might actually create buffers on either side that you don't know about. In the end, new actually uses malloc, but it does other stuff too, and like monarch said, it can be overloaded.

    You can actually do your allocation of objects with malloc/free if you really want to:
    Code:
    Object * obj = new((Object*)malloc(sizeof(Object))) Object();
    
    obj -> ~Object();
    free(obj);
    But that's only really used for advanced memory management like memory pools and such. It can be significantly faster, but not always.

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Issue regarding malloc and free in C++

    Quote Originally Posted by mayank_3103 View Post
    Thanks for the reply and I already applied in my demo code, and it worked. Though It might not destroyed the object, but It didnt shown me any error.
    There are many things in C++ that will not show you an error, even though it's wrong. It is not guaranteed that C++ will tell you that what you're doing is incorrect -- you must know that for yourself, and not rely on the running of a program to tell you this.
    So there must be any specific reason to avoid this type of programming, or its just the rule to use delete with new and free with malloc?
    delete calls the destrucctor oif the object, free() does not. If you've written a user-defined destructor, that destructor will not get the chance to run, and you can have all sorts of things wrong happening.
    Somewhere I read not to mix C and C++ concepts together as malloc is in C and new is given by C++, but I dont think this is the main reason.
    The main reason is that new and malloc are not the same thing. The former creates objects dynamically, the latter does not. All malloc() does is give you a bunch of bytes. Nothing more than that. It doesn't give you an object properly constructed.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured