using malloc or new in C/C++

This is a discussion on using malloc or new in C/C++ within the C Programming forums, part of the General Programming Boards category; Hello World! I am a self-study about C/C++ programming. when learning about dynamic allocation, I wonder about using malloc and ...

  1. #1
    C-Dumbie
    Guest

    using malloc or new in C/C++

    Hello World!

    I am a self-study about C/C++ programming. when learning about dynamic allocation, I wonder about using malloc and
    new.
    Does anyone know what the difference between MALLOC and
    NEW? Does NEW have a advantage over MALLOC such as
    safe memory allocation...better performance or something...?
    Thanx for your help in advance.

    C-Dumbie

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    malloc is the c way of getting memory from the heap.
    new is the c++ way of doing the same thing but new will also call the classes constructor. Also new is an operator and as such can be overloaded which is occaisionally useful.
    new is not available in c but malloc() is available in c++ but new should be prefferred due to constructor calling.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    I'm not too sure, but i think that new is just malloc wrapped in a more attractive package.

    One thing, never use free with new and never use delete with malloc.

    Always use them in pairs.
    IOW - new && delete
    malloc && free

    And if you new anything with square brackets, then you must delete it with square brakets, eg.
    Code:
    char* myName;
    myName = new char[100];           //Note the brackets
    strcpy(myName,"Weed Eater");
    cout << myName;
    delete [] myName;                       //Note the brackets

  4. #4
    C-Dumbie
    Guest
    A friend of mine explaining that using malloc() is more effcient
    than NEW operator because NEW cause an overhead due to
    constructor call. Is he right? Can you guys C experts explain
    clearly about that. Which one I would prefer to use? malloc or
    new?

    C-Dumbie
    I use borland C/C++ version 5.1 compiler

  5. #5
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    The overhead is barely noticeable with a profiler, much less in a running program. Allocating memory takes time no matter how it works, so use malloc/free with C and new/delete with C++.
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

  6. #6
    C-Dumbie
    Guest
    Originally posted by Dr. Bebop
    The overhead is barely noticeable with a profiler, much less in a running program. Allocating memory takes time no matter how it works, so use malloc/free with C and new/delete with C++.
    Thanx for your fast response as speed of light. :-)

    Since I try to code some programs playing with I/O ports using C/C++ borland compiler which accepts both malloc and new. Which one you would recomend me to use?
    Again, thanx
    C-Dumbie

  7. #7
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    Same as above, ifyou're writing pure C then use malloc/free, if you're writing C++ then use new/delete.
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

  8. #8
    Evil Member
    Join Date
    Jan 2002
    Posts
    638

    Another advantage of new...

    It has been mentioned that malloc does not call constructors, whereas new does. This is true.

    But new does something else of note.

    A call to malloc when there is no more m to alloc returns a NULL pointer. A call to new when there isn't any new left throws a bad_alloc. This makes it MUCH easiar to deal with memory exhaustion (granted there isn't a FYOS-load you can do about it, but it keeps you from accidentally forgetting to check the return value on a malloc and dereferencing a NULL pointer.

    That said, new does add a statistcally insignificant amount of overhead. You really ought not to be dynamically allocating memory so often that it becomes problematic anyway.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  2. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 11:26 AM
  3. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  4. malloc and realloc
    By odysseus.lost in forum C Programming
    Replies: 3
    Last Post: 05-27-2005, 08:44 AM
  5. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 08:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21