Code reuse in C data structures
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?
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).
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).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement