S
Stijn van Dongen
A question about void*. I have a hash library where the hash create
function accepts functions
unsigned (*hash)(const void *a)
int (*cmp) (const void *a, const void *b)
The insert function accepts a void* key argument, and uses the
functions above to store this argument. It returns something (linked to
the key) that the caller can store a value in. The actual key argument
is always of the same pointer type (as seen in the caller, say foo*).
Now I would like to have a function, say hash_keys, which returns all
keys in the hash as a pointer to a malloced array of void*. However,
there is not really a type I can use. I see two possible solutions.
The first is to typedef a struct containing a void pointer:
typedef struct {
void* pp;
} genpp;
and have hash_keys return genpp* (the size could be written in an int*
argument
to hash_keys). This would make accessing the keys cumbersome.
Solution 2) is illegal C (I think), but it's tempting. hash_keys would
construct an array of void*-sized elements and copy its key pointers
there using char* arithmetic. It would return void* and the caller
would cast it to foo**. For one thing, this assumes sizeof(void*) is
sizeof(foo*) for all possible foo. For another, I see nothing in the
standard on void* that would support any of this. However, it feels as
if approach 2) is conceptually extremely similar to the first and I
suspect it would work on nearly all platforms/compilers.
The questions then are,
1. Did I miss another solution where hash_keys creates some array?
2. Is my assessment of 2) correct?
3. On what kind of platforms/compilers would 2) fail? Outside the scope
of this newsgroup, I know, but perhaps some kind soul can comment.
regards,
Stijn
function accepts functions
unsigned (*hash)(const void *a)
int (*cmp) (const void *a, const void *b)
The insert function accepts a void* key argument, and uses the
functions above to store this argument. It returns something (linked to
the key) that the caller can store a value in. The actual key argument
is always of the same pointer type (as seen in the caller, say foo*).
Now I would like to have a function, say hash_keys, which returns all
keys in the hash as a pointer to a malloced array of void*. However,
there is not really a type I can use. I see two possible solutions.
The first is to typedef a struct containing a void pointer:
typedef struct {
void* pp;
} genpp;
and have hash_keys return genpp* (the size could be written in an int*
argument
to hash_keys). This would make accessing the keys cumbersome.
Solution 2) is illegal C (I think), but it's tempting. hash_keys would
construct an array of void*-sized elements and copy its key pointers
there using char* arithmetic. It would return void* and the caller
would cast it to foo**. For one thing, this assumes sizeof(void*) is
sizeof(foo*) for all possible foo. For another, I see nothing in the
standard on void* that would support any of this. However, it feels as
if approach 2) is conceptually extremely similar to the first and I
suspect it would work on nearly all platforms/compilers.
The questions then are,
1. Did I miss another solution where hash_keys creates some array?
2. Is my assessment of 2) correct?
3. On what kind of platforms/compilers would 2) fail? Outside the scope
of this newsgroup, I know, but perhaps some kind soul can comment.
regards,
Stijn