Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » How to delete allocated memory at void pointer?

Credits go to amarillion, BAF, Billybob, HoHo, Hrvoje Ban, Kitty Cat, miran, OICW, Steve++, and Wil Renczes for helping out!
This thread is locked; no one can reply to it. rss feed Print
 1   2 
How to delete allocated memory at void pointer?
Onewing
Member #6,152
August 2005
avatar

Quote:

You're assuming that the linked list contains only objects of a specific type.

What object can void* not point to?

Quote:

Also what if you want to store ints, for example? You'd need one pointer dereference every time a value is accessed while that's not the case if you'd use a template.

And what's so bad about dereferencing? Also, void* and the next* is not all I store inside the linked list. I also store an int (ikey) for sorting purposes (or if you are simply storing ints) as well as a char* (ckey) if you need a char for some reason. Of course, this makes each node larger in size, but I don't think it's enough to make anything suffer.

------------
Solo-Games.org | My Tech Blog: The Digital Helm

Evert
Member #794
November 2000
avatar

Quote:

That's not a good use for void*

It is.
That is to say, in C. :P In C++, one should (almost?) never need to use void pointers. If there's a situation where they seem nescessary it's probably a sign that you need to rethink your design.

Fladimir da Gorf
Member #1,565
October 2001
avatar

Quote:

What object can void* not point to?

That's my point indeed. When you need to access the data you need to know the real type of the objects. But you can't know it for sure because the type information is lost.

Quote:

And what's so bad about dereferencing?

It's slower than a direct access.

Quote:

I also store an int (ikey) for sorting purposes (or if you are simply storing ints) as well as a char* (ckey) if you need a char for some reason. Of course, this makes each node larger in size, but I don't think it's enough to make anything suffer.

Or you could store an object which contains all that data and a pointer to the real object or to store objects which inherit from the same virtual base class.

Quote:

It is.
That is to say, in C.

But the question was (indirectly) if there's any good use for void pointers in C++.

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

Onewing
Member #6,152
August 2005
avatar

Quote:

It is.
That is to say, in C. In C++, one should (almost?) never need to use void pointers. If there's a situation where they seem nescessary it's probably a sign that you need to rethink your design.

I wrote the linked list structure back in my C-hayday of early college years. When I gradually moved into C++, I just kept using it because it was easy to use. I guess I should rethink how to do it then...hmmmm...

------------
Solo-Games.org | My Tech Blog: The Digital Helm

HoHo
Member #4,534
April 2004
avatar

Quote:

And what's so bad about dereferencing?

Compared to not dereferencing it is dead slow and takes more memory.

When extreme performance is needed then there is a difference. On the other hand, if extreme performance is needed it is usually best to stay away from lists :P

[edit]

Quote:

I guess I should rethink how to do it then...hmmmm...

As I assume you already know how to code a linked list you might want to swich over to STL list. With that you automatically get all sorts of nifty things from <algorithm> header too. A slight speed improvement is also quite probable :)

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

Onewing
Member #6,152
August 2005
avatar

Quote:

That's my point indeed.

I see. Well, I just got use to my class because it worked so well and I didn't really know I was losing that much performance. Of course, I never had a very big list so it's not like I'd notice a performance flaw anyway. My original concept of making the linked list structure was "store something in a generic list" (some_list->data = something) and "read information from the list" (dereference). Out of the few years I've been using it, I've never accidentally casted it to the wrong type when I dereferenced it. Of course, this is all old news now and I'm going to research STL and I guess, put my list to rest. :)

Quote:

As I assume you already know how to code a linked list you might want to swich over to STL list.

Done and done. I'm gonna tinker around with this when I get a chance. Is this a good site to start from?

------------
Solo-Games.org | My Tech Blog: The Digital Helm

HoHo
Member #4,534
April 2004
avatar

Quote:

Is this a good site [msoe.edu] to start from?

Seems to be. Though I usually just use whatever is first result from Google :)

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

Onewing
Member #6,152
August 2005
avatar

Quote:

Though I usually just use whatever is first result from Google

It was. ;)

------------
Solo-Games.org | My Tech Blog: The Digital Helm

HoHo
Member #4,534
April 2004
avatar

googling for "c++ iostream" or "stl vector" come up with totally different sites. I usually just google for specific stuff and use several different sites for reference

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

Onewing
Member #6,152
August 2005
avatar

try "stl list"

------------
Solo-Games.org | My Tech Blog: The Digital Helm

 1   2 


Go to: