-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hello, I have a simple question, and after a *very long* google search
I still can't get it.
It's about generic abstract data types (for example, a list). I've
coded it in ADA, and its quite easy to do, but there is a thing I cannot
decide when doing it in C. This is about the actual data storing in a
node. I've seen lots of examples where what is stored is the pointer to
data, but I think it would be better to store the actual object (of
course, you need the element size). For example, this can be the node:
struct node {
void *object;
struct node *next;
}
struct ADT {
struct node *head;
size_t obj_size;
}
and a function could be, for example:
int myfunction (struct ADT *list, void *elem)
{
struct node *tmp;
...
tmp = malloc (sizeof (struct node));
/* Option 1: examples seen googling, store pointer */
tmp->object = elem;
/* Option 2: Seen 1 or 2 times, copying the object itself */
memcpy (&(tmp->object), elem, list->obj_size);
...
I personally think Option 2 is more useful, because you can mess with
stored items by modifying the element in the caller function (please,
correct me :)
The implementation can also change, without using memcpy, by passing a
user-supplied object copy function, this is trivial (I think).
Just looking for some advice, how could you do it? TIA
- --
Alberto Giménez, SimManiac en el IRC
http://www.almorranasozial.es.vg
GnuPG ID: 0x3BAABDE1
Don't compare Linux with Windows. There's no colour (except blue).
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org
iD8DBQFAlCo20keCtzuqveERAgBOAJ9OUDqLSOE+7ooKdkoxzF Toh0mJdwCfXENC
57YRs3DhVy2+NzxN0829v7k=
=Cs6G
-----END PGP SIGNATURE-----