Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » destroy vs. delete

This thread is locked; no one can reply to it. rss feed Print
destroy vs. delete
#00JMP00
Member #14,740
November 2012

As I couldn't find usefull information in the manual, I wonder if someone knows the difference, if there is.

bitmaps can be removed with the

delete bitmapname or
destroy_bitmap() function.

Is there a difference and which one is better??

torhu
Member #2,727
September 2002
avatar

Use destroy_bitmap(). delete doesn't know anything about Allegro's bitmaps, and might or might not do the wrong thing. delete is only for freeing memory allocated with new.

#00JMP00
Member #14,740
November 2012

Thank you for the answer.

Ever since I am here, I wonder why you are that grim looking.
Maybe you should visit a dentist or something :-))

torhu
Member #2,727
September 2002
avatar

It's just the way I have always been >:(

#00JMP00
Member #14,740
November 2012

Ah, ok.

Maybe some noobs are intimidated by the avatar :-))

Dizzy Egg
Member #10,824
March 2009
avatar

...cool! This may be the contender for the nest noob intro post yet!!! :D

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

torhu
Member #2,727
September 2002
avatar

I wish you the best of luck >:(

bamccaig
Member #7,536
July 2006
avatar

To go more into detail, Allegro is written in C. The standard C functions for allocating and freeing memory on the heap are the malloc family and free. C++ uses new and delete operators to instantiate objects on the heap and destroy/free them. Both can give you a pointer to a chunk of memory representing some data, but they are incompatible with one another. You cannot free something you allocated with new or delete something that you allocated with malloc. I'm not sure what Allegro uses under the surface for memory allocation, but more than likely it is malloc-based. Even if it isn't, it is certainly not new/delete-based since those are C++ constructs.

To go even more into detail, malloc and free purely manage memory. They allocate memory and release it respectively, but that's all they do. They don't initialize the memory in any way. In C, you typically have a function that initializes a structure's members. That is what al_load_bitmap does, for example. There is generally a matching function for API creation functions like this to destroy the "thing" and release the memory. For example, al_destroy_bitmap. The new operator in C++ does more than just allocate memory. It also executes a class' constructor, which initializes the object's members, and possibly carries out some logic. Similarly, the delete operator first calls a class' destructor before releasing the memory which may release internal resources or carry out some logic. They are different animals with similar roles. As a rule, when you're writing C code you should use the malloc and free family of functions, and in C++ you should use the new and delete operators. With a C library like Allegro, you should be using the library API to create, initialize, and destroy data (and generally access the data). When writing your own C code it's probably a good idea to wrap your creation/destuction code into reusable functions too.

#00JMP00
Member #14,740
November 2012

>bamccaig:
>To go more into detail, Allegro is written in C. The standard C functions for >allocating and freeing memory on the heap are the malloc family and free. C++ uses >new and delete operators to instantiate objects on the heap and destroy/free them. >Both can give you a pointer to a chunk of memory representing some data, but they >are incompatible with one another. You cannot free something you allocated with new >or delete something that you allocated with malloc. I'm not sure what Allegro uses >under the surface for memory allocation, but more than likely it is malloc-based. >Even if it isn't, it is certainly not new/delete-based since those are C++ >constructs.

>To go even more into detail, malloc and free purely manage memory. They allocate >memory and release it respectively, but that's all they do. They don't initialize >the memory in any way. In C, you typically have a function that initializes a >structure's members. That is what al_load_bitmap does, for example. There is >generally a matching function for API creation functions like this to destroy the >"thing" and release the memory. For example, al_destroy_bitmap. The new operator in >C++ does more than just allocate memory. It also executes a class' constructor, >which initializes the object's members, and possibly carries out some logic. >Similarly, the delete operator first calls a class' destructor before releasing the >memory which may release internal resources or carry out some logic. They are >different animals with similar roles. As a rule, when you're writing C code you >should use the malloc and free family of functions, and in C++ you should use the >new and delete operators. With a C library like Allegro, you should be using the >library API to create, initialize, and destroy data (and generally access the >data). When writing your own C code it's probably a good idea to wrap your >creation/destuction code into reusable functions too.

Next time, you should offer some water along with this dry cake :-)

bamccaig
Member #7,536
July 2006
avatar

{"name":"844149.gif","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/9\/4\/94f817383a007456118f49a5afad4bda.gif","w":499,"h":266,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/9\/4\/94f817383a007456118f49a5afad4bda"}844149.gif
{"name":"yoda_noob_12275-s600x400-48271.jpg","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/1\/615818b8a51c7ef88dea79b6a33f3b82.jpg","w":600,"h":400,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/1\/615818b8a51c7ef88dea79b6a33f3b82"}yoda_noob_12275-s600x400-48271.jpg

torhu
Member #2,727
September 2002
avatar

Go to: