array of void*

M

Martijn

Hi,

I am looking over some of my old implementations. One of them has an
"array" of void pointers defined as a pointer to the first element (without
getting stuck on nomenclature, I am aware of the difference between a
pointer and an array).

This pointer in effect is a pointer to a void pointer: void**. But iirc,
this is not entirely valid and may become a problem when dereferencing and
doing pointer arithmetic. And in all honesty, it would still all compile
fine if I wrote void* instead of void**. The "array" has to be dynamic in
nature, that is, I need to resize it (using realloc) and the data stored is
represented as a void* to allow different types of pointers to be stored.

What is the proper way of going about implementing this? Thanks for any
pointers and if I've missed anything in the faq, let me know.
 
R

Richard Bos

Martijn said:
I am looking over some of my old implementations. One of them has an
"array" of void pointers defined as a pointer to the first element (without
getting stuck on nomenclature, I am aware of the difference between a
pointer and an array).

This pointer in effect is a pointer to a void pointer: void**. But iirc,
this is not entirely valid

It's quite valid.
and may become a problem when dereferencing and
doing pointer arithmetic.

Nope. A void * is an object. It is a pointer that does not point at any
kind of object, but the pointer itself is an object alright. Because of
this, a void ** is a perfectly normal object pointer, and doing pointer
arithmetic on one will work normally. For example, void_p_p++ will
increase void_p_p by sizeof(void *) bytes, just as int_p++ will increase
int_p by sizeof(int) bytes.
It is only void * _itself_ which cannot be used for pointer arithmetic,
because "a void object" has no size (and, AFAICT, doesn't actually
exist).
And in all honesty, it would still all compile
fine if I wrote void* instead of void**.

Yes, but that would be semantically incorrect, and might well invoke
undefined behaviour, depending on what your code actually does.

Richard
 
K

Kevin Easton

Martijn said:
Hi,

I am looking over some of my old implementations. One of them has an
"array" of void pointers defined as a pointer to the first element (without
getting stuck on nomenclature, I am aware of the difference between a
pointer and an array).

This pointer in effect is a pointer to a void pointer: void**. But iirc,
this is not entirely valid and may become a problem when dereferencing and
doing pointer arithmetic. And in all honesty, it would still all compile
fine if I wrote void* instead of void**. The "array" has to be dynamic in
nature, that is, I need to resize it (using realloc) and the data stored is
represented as a void* to allow different types of pointers to be stored.

What is the proper way of going about implementing this? Thanks for any
pointers and if I've missed anything in the faq, let me know.

If you mean you have:

void **p = malloc(10 * sizeof *p);

and then you access the void * objects p[0] to p[9], then that's fine
and correct.

- Kevin.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,079
Messages
2,570,573
Members
47,204
Latest member
MalorieSte

Latest Threads

Top