Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

wolverine

Code reuse in C data structures

This topic is 6236 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Why is it that in most books about C, the implementation of linked lists (for example) are for one particular type? I mean, most of the books have an implementation of linked lists for holding one integer per node or something like that. I know that C wasn''t develpoed thinking in code reuse, but.... Is there any *big* problem in just using void* in the nodes to contain the data? For example, my implementation of an ordered linked list had a node* for the head and a pointer to a function wich received two void* and compared them giving an integer for a result. Of course it would be a bit slower, since you would have to make casts everytime you wanted to insert or something like that, but that''s better then rewritting the whole them thing, isn''t it?

Share this post


Link to post
Share on other sites
Advertisement
I don''t know about most books, but I do know of certain books on C that are written by some really dumb people (any book by Barry Holmes, stay away from them).


Using a void pointer is the only way to create reusale data structures in C. The only problem is that a void pointer isn''t type-safe (you could place anything in there) and it requires that you store the size of the variable(which is a hassle).

The casts are done at compile time, thus they shouldn''t incur any performance penalty (unless it is a conversion from a float/double to an int).

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!