 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Thu Mar 29, 2007 6:23 am Post subject: [comp.lang.c.moderated] Attempt at typesafe vector, comments |
|
|
Hi,
Here is my attempt at a typesafe vector 'object' for C. There are
certainly some tricky aspects of the code, but I think these are
confined to the header file and (I hope) wont create problems for users.
I am re-inventing the (bad) wheel here?
Do you have any comments or advice for me regarding possible problems
and improvements?
Thanks,
Doug Eleveld
/* Vector interface and implementation */
#include <stdlib.h>
typedef struct {
void* data; /* This MUST be first */
unsigned len;
} GENERICVECTOR;
#define VECTOR(a) \
union { \
a* data; \
GENERICVECTOR vector; \
}
void extendvector5(GENERICVECTOR* v, unsigned newlen, size_t datasize)
{
/* For now just pretend realloc never fails */
if (v->len < newlen)
v->data = realloc(v->data, newlen * datasize);
}
#define EXTENDVECTOR5(v,newlen) extendvector5(&v->vector, newlen,
sizeof(v->data[0]))
/* Vector demo code */
#include <stdio.h>
#include <string.h>
typedef struct {
int x;
} DATA;
typedef VECTOR(DATA) VECT;
int main(void)
{
int i, j;
VECT* ptr;
VECT vector[2];
memset(&vector, 0, sizeof(vector));
ptr = vector;
for (j=0; j<2; j++)
EXTENDVECTOR5(ptr++, 20); /*This looks dangerous but is safe */
for (j=0; j<2; j++) {
for (i=0; i<vector[j].len; i++) {
vector[j].data[i].x = i;
printf(“%i “, vector.data[i].x);
}
}
return EXIT_SUCCESS;
}
But wait, you say! EXTENDVECTOR5 is not a safe macro because it
evaluates its first argument twice, once for the first argument and
again for the second argument. From the previous example:
EXTENDVECTOR5(ptr++, 20);
expands to:
extendvector5(&(ptr++)->vector, (20), sizeof((ptr++)->data[0])))
It is very counterintuitive, but ptr is only incremented once here!
This is because everything inside sizeof() is evaluated at
compile-time, not at run-time. During compilation sizeof() code gets
replaced with a constant and the run-time code never sees the second
ptr++. In a sense, the second expansion ptr++ in the second argument
gets calculated and then discarded before the program even starts.
Because the ptr++ only happens at compile time and not at run time it
does not actually change the ptr variable. So the EXTENDVECTOR5 macro
is actually safe to use in this way.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Douglas A. Gwyn Guest
|
Posted: Thu Apr 19, 2007 7:14 pm Post subject: Re: [comp.lang.c.moderated] Attempt at typesafe vector, comm |
|
|
Hans-Bernhard Bröker wrote:
| Quote: | ... There's
simply no way to inquire the type of an object in C, so no way to check
for wrong types.
|
Well, that's overstating the case. C doesn't have built-in support
for such things, but they can be layered on within the "object"
implementation. Indeed, C++ started out as a preprocessor that
produced C code, so it's "doable" if you impose extra structure.
| Quote: | #define DEFINE_VECTOR_TYPE(type) \
typedef struct {type *ptr; size_t n; } vector_of_##type;
|
You could also use a constructor/initializer function and embed
a stringization of the type as another field, which would permit
run-time type checking.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Powered by phpBB © 2001, 2006 phpBB Group
|