Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » if malloc and remalloc are on a boat...

This thread is locked; no one can reply to it. rss feed Print
 1   2   3 
if malloc and remalloc are on a boat...
kazzmir
Member #1,786
December 2001
avatar

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
avatar

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
avatar

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
avatar

Mainly, new and delete call the constructors and destructors. malloc and free don't (for obvious reasons). Read here for more ....

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

kazzmir
Member #1,786
December 2001
avatar

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
avatar

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 ...

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Marty Dill
Member #277
April 2000
avatar

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
avatar

Does attempting to allocate an obscure amount of memory also create a null pointer, like malloc?
for example, attemtping to create an array of 65000 BITMAP's? ::)

"He who controls the stuffing controls the Universe"

Marty Dill
Member #277
April 2000
avatar

It depends.
new is supposed to throw an exception when an allocation fails; however, some implementations such as MSVC6 will return NULL instead (this can be fixed with _set_new_handler()).

23yrold3yrold
Member #1,134
March 2001
avatar

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!

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

kazzmir
Member #1,786
December 2001
avatar

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
avatar

Quote:

in the case that in the future NULL is changed from 0 to something else?

Like what?

I use NULL, anyway ....

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Marty Dill
Member #277
April 2000
avatar

Heh ... see this GameDev thread. 0 will always be safe. The same can't be said for NULL :)

kazzmir
Member #1,786
December 2001
avatar

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
avatar

Umm..
as far as I know, NULL is equivalent to 0 in C++.
In an MSVC Header I was looking through (I can't remember which), NULL is defined something like:

#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
avatar

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

__________________________________________________
Skoobalon Software
[ Lander! v2.5 ] [ Zonic the Hog v1.1 ] [ Raid 2 v1.0 ]

23yrold3yrold
Member #1,134
March 2001
avatar

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() ....

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

StevenVI
Member #562
July 2000
avatar

C++ and all it's fancy differences... :P I don't even know what that means :P.

..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

__________________________________________________
Skoobalon Software
[ Lander! v2.5 ] [ Zonic the Hog v1.1 ] [ Raid 2 v1.0 ]

23yrold3yrold
Member #1,134
March 2001
avatar

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++.

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Derezo
Member #1,666
April 2001
avatar

STL isn't as easy as malloc/realloc tho.
I did read your articles in an attempt to learn some C++ :) It was slightly successful.. but once you stepped into the iterators I got lost. Way lost. I was on a whole new planet...
I just decided to reinvent the wheel and make and do a huge linked list thing....oh boy.. I got so confused.. I have linked lists inside of linked lists that have linked lists inside of their linked lists.. Seriously :P There's like 6 layers of linked lists ::) Works nicely though.

As I understand it, new has a sizeof() built into it :P

"He who controls the stuffing controls the Universe"

StevenVI
Member #562
July 2000
avatar

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

__________________________________________________
Skoobalon Software
[ Lander! v2.5 ] [ Zonic the Hog v1.1 ] [ Raid 2 v1.0 ]

23yrold3yrold
Member #1,134
March 2001
avatar

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 ;D

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];

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Derezo
Member #1,666
April 2001
avatar

:D 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?
I can't remember how to use realloc right now.. but I remember it's just as easy as malloc..

"He who controls the stuffing controls the Universe"

StevenVI
Member #562
July 2000
avatar

Hmm.. wow... that does look easy :P.

I'm still using C though :P.

realloc 101:

FOO *bar = malloc(sizeof(*FOO));
bar = realloc(bar,sizeof(*FOO) * 2);

-Sven

__________________________________________________
Skoobalon Software
[ Lander! v2.5 ] [ Zonic the Hog v1.1 ] [ Raid 2 v1.0 ]

kazzmir
Member #1,786
December 2001
avatar

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.

 1   2   3 


Go to: