|
This thread is locked; no one can reply to it. |
1
2
|
C++ question... malloc or new |
Dario ff
Member #10,065
August 2008
|
I have always been wondering of this while programming. I use C++, but i like using the malloc function for allocating my objects, but i don´t know difference between using new and malloc (differences on runtime process, not writing). so, my question is, if i use C++, should i always use the new function or i can use malloc as well? I would really like an answer to this, since I always had this doubt. Thnx for reading... TranslatorHack 2010, a human translation chain in a.cc. |
bamccaig
Member #7,536
July 2006
|
AFAIK, malloc just allocates memory. The new keyword allocates memory and initializes the object (i.e. calls the constructor) AKA instantiates an object. It's the difference between having an instance of Foo and having enough memory for an instance of Foo. If you use malloc to create a Foo, then what you really have is a pointer to sizeof(Foo) bytes of garbage, not a Foo. I assume for C++ code you should be using new/delete and for C-only code using malloc/free. -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
Dario ff
Member #10,065
August 2008
|
oh ok, but i don´t use too much constructors myself, so i think i will stick with malloc. thnx TranslatorHack 2010, a human translation chain in a.cc. |
Audric
Member #907
January 2001
|
Quote: if i use C++, should i always use the new function
IMO, when you're using classes, you should always use new and delete. |
OICW
Member #4,069
November 2003
|
I'd reccomend using new and delete if you use C++. If you want complete control like with malloc, I remember there's something called allocator class, which should provide you with malloc functionality. And as far as objects go, you should use new and delete any time. [My website][CppReference][Pixelate][Allegators worldwide][Who's online] |
OnlineCop
Member #7,919
October 2006
|
I'm in favor of 'new' and 'delete' because of the ability of C++ to overload functions. If your class inherits any others, it's very possible that those other classes could have constructors/destructors. 'malloc' and 'free' have their place for C, but for C++, everyone who has ever discussed this topic with me has strongly opted for 'new' and 'delete'.
|
Mokkan
Member #4,355
February 2004
|
You should use new and delete with classes at least. I'm not sure why you'd use malloc over new in any other case either... EDIT: Wow, I got way beaten.
|
bamccaig
Member #7,536
July 2006
|
samo the thief said: oh ok, but i don´t use too much constructors myself, so i think i will stick with malloc. thnx
Keep in mind that when you say... A constructor goes one step further and sets all members to known default values. Even in C, you can use a constructor concept to initialize a structure, either by wrapping the creation into a function (e.g. create_foo) or by passing the existing pointer into a function (e.g. init_foo) and setting all members to known default values. Either way, you may need to be careful if you're just going to leave it as garbage. Unless your application requires the finite optimization of not writing unnecessarily to those memory blocks (and in 99.999999999% of cases it won't), I would recommend you either zero the memory or call some kind of initializer/constructor. C++ makes this an easy and intuitive process so why fuck with that? -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
Dario ff
Member #10,065
August 2008
|
i was talking about malloc because I learned some c way back ago, but now i started using C++, and i got that little doubt. well, suppose I´m going to have a nice day today with replacing some part of my code XD TranslatorHack 2010, a human translation chain in a.cc. |
Kitty Cat
Member #2,815
October 2002
|
Quote: The members of the Foo that foo points to are garbage. Also note that if your class contains other classes (eg. std::string, std::vector), using malloc will NOT initialize those object properly and can crash when you try to use them. -- |
aj5555
Member #9,033
September 2007
|
there are other alternatives. |
Speedo
Member #9,783
May 2008
|
IMO, it's utterly foolish to write C++ code with malloc/free. If you mix them with new/delete in your code, you risk quite a bit of confusion for yourself or anyone else later on (do I need to free this pointer or delete it? can't interchange the two). If you go entirely malloc/free you're begging for problems initializing C++ objects. And the big question is... what is malloc doing for you that new is incapable of doing? |
Onewing
Member #6,152
August 2005
|
Quote: there are other alternatives. Sounds like you're offering to sell him watches in a dark alley... I personally love new and delete and said goodbye to malloc/free a while ago. The only times I have any difficulty (have to refresh myself) is when I create a multidimensional array with new. ------------ |
Tobias Dammers
Member #2,604
August 2002
|
Multidimensional arrays in C are a brain-wrecking piece of crap anyway. --- |
Timorg
Member #2,028
March 2002
|
if your array is of pointers, or they are all positive integers, you could use the [] operator and return -1 or NULL ____________________________________________________________________________________________ |
Tobias Dammers
Member #2,604
August 2002
|
Yes and no. --- |
axilmar
Member #1,204
April 2001
|
Quote: so, my question is, if i use C++, should i always use the new function or i can use malloc as well? Forget malloc and free, use new. Don't use delete, use boost::shared_ptr. You might be a newbie, but you will regret it later if you don't do so. It takes a while to learn boost, but it pays off hugely. |
Tobias Dammers
Member #2,604
August 2002
|
I say, manual memory management builds character. --- |
Trezker
Member #1,739
December 2001
|
#include <boost/shared_ptr.hpp> typedef boost::shared_ptr<Myclass> MyclassPtr; MyclassPtr myclassinstance(new Myclass); Voila, instant garbage collection! I'm rather dissatisfied with the naming of shared pointer types though. Any tips? |
bamccaig
Member #7,536
July 2006
|
classname_ptr and classname_weakptr? -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
Trezker
Member #1,739
December 2001
|
Maybe you're right... |
amber
Member #6,783
January 2006
|
Quote: what is malloc doing for you that new is incapable of doing? realloc? (I actually don't know. I'd be curious if any people more knowledgable in C++ could weigh in...) |
Speedo
Member #9,783
May 2008
|
Quote: realloc? (I actually don't know. I'd be curious if any people more knowledgable in C++ could weigh in...) It seems to me like the primary use of realloc (at least in C++) would be to resize a dynamic array. Of course, 98% of C++ programmers would then tell you to use vector or another STL container, but even if you insisted on using arrays you could duplicate realloc with a little template magic:
|
axilmar
Member #1,204
April 2001
|
The memcpy you do is bad, very bad. You should never do that with C++ objects. You should always use the assignment operator to copy the data. |
Matt Smith
Member #783
November 2000
|
Quote: If you mix them with new/delete in your code, you risk quite a bit of confusion for yourself or anyone else later on (do I need to free this pointer or delete it? can't interchange the two) IMO, That's a sound reason for sticking to C. The raison d'etre for C++ is that the OOP model is standard. For this reason alone, you should use new/delete when writing classes. |
|
1
2
|