|
if malloc and remalloc are on a boat... |
kazzmir
Member #1,786
December 2001
|
what is the technical difference between malloc and new? and also the difference between delete and free. I understand that you should use delete when you new stuff and free when you malloc stuff, but what happens when you delete something malloced or free something newed? my programs sometimes crash at random times becuase i forget that i malloced something and then try to delete it later, or maybe its something else. |
Derezo
Member #1,666
April 2001
|
I've heard no technical difference (from other threads).. other than you need to use delete to get rid of new'd vars.. and free to get rid of malloc'd vars.. "He who controls the stuffing controls the Universe" |
H
Member #1,673
November 2001
|
new is the C++ keyword to do the same thing as malloc, but in a better way. C++ programs should only use new and delete. malloc and free are only supported for compatibility. |
23yrold3yrold
Member #1,134
March 2001
|
Mainly, new and delete call the constructors and destructors. malloc and free don't (for obvious reasons). Read here for more .... -- |
kazzmir
Member #1,786
December 2001
|
23yearold: that website you posted was very helpful, but i noticed something that i disagree with. it says that if you new a pointer, you dont have to check to see if its null before you delete it. I have come across this problem hundreds of times and know for a fact that if i try to delete something that is null my program crashes. I am using gcc 2.95 i think, but also have tried it using gcc 3.x something on djgpp. |
23yrold3yrold
Member #1,134
March 2001
|
Quote: I have come across this problem hundreds of times and know for a fact that if i try to delete something that is null my program crashes. Funny; I've never had that happen to me. Anyone else? I thought it was a guarentee of the language ... -- |
Marty Dill
Member #277
April 2000
|
I very much doubt that. The C++ standard states quite explicitly that applying delete to 0 has no effect, and I've never seen a compiler that doesn't follow this part of the standard. Perhaps you are confusing a null pointer with an uninitialized pointer. Run this program and see what happens: int main(int argc, char* argv[]) { char* p = 0; delete p; return 0; }
|
Derezo
Member #1,666
April 2001
|
Does attempting to allocate an obscure amount of memory also create a null pointer, like malloc? "He who controls the stuffing controls the Universe" |
Marty Dill
Member #277
April 2000
|
It depends. |
23yrold3yrold
Member #1,134
March 2001
|
Quote: Unlike malloc(), new never returns NULL! Therefore you should simply write: Fred* p = new Fred(); // No need to check if p is NULL However, if your compiler is old, it may not yet support this. Find out by checking your compiler's documentation under "new". If you have an old compiler, you may have to force the compiler to have this behavior. From my link above .... EDIT: Matthew, allow code in quotes! -- |
kazzmir
Member #1,786
December 2001
|
hmm, good point. maybe i was deleting something that was already deleted or unitialized. its still nice to be safe and check if p == null before you delete it. isnt it a bad thing to make pointers equal to 0 instead of NULL in the case that in the future NULL is changed from 0 to something else? |
23yrold3yrold
Member #1,134
March 2001
|
Quote: in the case that in the future NULL is changed from 0 to something else? Like what? I use NULL, anyway .... -- |
Marty Dill
Member #277
April 2000
|
Heh ... see this GameDev thread. 0 will always be safe. The same can't be said for NULL |
kazzmir
Member #1,786
December 2001
|
i guess NULL will probabyl never not be 0, and i havent read the c++ standard ever, but just in case that computers become radically different. integers on all machines are not the same size, and the same could be said about NULL. in the level of abstraction for pointers, NULL is the nothing value. any value, such as 23409 or 0, should be able to be used for any program to work that uses NULL instead of 0. |
Derezo
Member #1,666
April 2001
|
Umm.. #define NULL 0
then there was some extern "C" thing to define it as (void*) something or other... So, wouldn't this be the same: int *myPointer = NULL; // or.. int *myPointer = 0;
"He who controls the stuffing controls the Universe" |
StevenVI
Member #562
July 2000
|
Quote: Unlike malloc(), new never returns NULL! Therefore you should simply write: Fred* p = new Fred(); // No need to check if p is NULL Hmm. If it never returns NULL, how can one determine if there was an error or not? For example, what happens when you try new'ing 10,000 GBs of data? What does it return then? Also, malloc is superior to new, because where malloc has it's counterpart, realloc, new has no 'renew' to update a chunk of allocated memory. -Sven __________________________________________________ |
23yrold3yrold
Member #1,134
March 2001
|
Quote: Hmm. If it never returns NULL, how can one determine if there was an error or not? It throws an exception. And I've never really had a use for realloc() .... -- |
StevenVI
Member #562
July 2000
|
C++ and all it's fancy differences... I don't even know what that means . ..and I use realloc in a couple of places in my (very large) project. I have many arrays that occationally need to be resized. Can't (easily) do that sort of thing in C++. If I can drag this a bit off-topic... I see the example code saying to type "Fred* p = new Fred();". How do you allocate memory from a function? I never understood how it works.. what's the exact syntax to use for new? In my opinion (not knowing C++), typing "my_int = malloc(bytes_to_allocate);" is much easier to understand. -Sven __________________________________________________ |
23yrold3yrold
Member #1,134
March 2001
|
Quote: I have many arrays that occationally need to be resized. Can't (easily) do that sort of thing in C++. Another poster who hasn't read my STL articles!! No card for you on Christmas .... On your last point; yes, it's a typo. It should be "Fred* p = new Fred;". I find malloc more difficult to read, especially since I have to cast it in C++. -- |
Derezo
Member #1,666
April 2001
|
STL isn't as easy as malloc/realloc tho. As I understand it, new has a sizeof() built into it "He who controls the stuffing controls the Universe" |
StevenVI
Member #562
July 2000
|
Ha.. don't assume, it makes an ass of u and me. I actually DID read your articles. Being a fan of C, (which looks so much prettier than C++ code IMO) I decided to stick with my methods of doing things. I got everything down, I don't need to learn new, alternate methods to confuse me ;p. But still.. so then the syntax would be "new DATA_TYPE" .. I assume you can also allocate an array with new, or are you forced to use vectors or whatever the C++ method is? -Sven __________________________________________________ |
23yrold3yrold
Member #1,134
March 2001
|
Quote: STL isn't as easy as malloc/realloc tho. Not only is it easier, it's automatic. You don't even have to give a thought to memory allocation; the class does it all behind the black curtian. You can control memory allocation if you want, like if you want to reserve memory ahead of time. Or you can just let it cruise on autopilot. I don't recommend using my articles as a primer to C++. I think I make a point of assuming you know the C++ basics, as well as templates. My first iterator example (2 lines of code) used 4 overloaded operators; I can see how this would be confusing Quote: Ha.. don't assume, it makes an donkey of u and me. I didn't, but you said resizing arrays can't be done easily in C++. Quote: I assume you can also allocate an array with new, or are you forced to use vectors or whatever the C++ method is? Of course: int *myptr = new int[256];
-- |
Derezo
Member #1,666
April 2001
|
I knew that, but I wanted to try to build a program and learn it like I did C Malloc is extremely easy though.. and STL looked a lot more complicated.. with it's classes and whatnot.. but, since I still don't understand it completely, I guess it's not my place to put it down.. but.. how could it be easier than malloc? "He who controls the stuffing controls the Universe" |
StevenVI
Member #562
July 2000
|
Hmm.. wow... that does look easy . I'm still using C though . realloc 101: -Sven __________________________________________________ |
kazzmir
Member #1,786
December 2001
|
malloc is a fun function to use as opposed to new which is boring and clunky. but new is the c++ version and c++ is more advanced and abstract than c. if you program in c, then i assume you dont like objects which are the most useful idea to ever come along in programming. |
|
|