G
Guest
I have a macro that can determine the number of elements in an array type. It is defined as follows
#define ARRAY_SIZE_NOGC(x) (sizeof(x) / sizeof(x[0])
This works with unmanaged array types, but not with pointers, since the size of a pointer is always the same and not the size of the data it points to. Obviously this doesn't work with managed types, which are all refered to by pointers. I could define the macro to handle managed arrays as follows
#define ARRAY_SIZE_GC(x) (x->Length
The problem is these two macros arn't generic. I would like one macro that would work for both. Something like the following
#define ARRAY_SIZE(x) ( IS_A_GC_POINTER(x) ? ARRAY_SIZE_GC(x) : ARRAY_SIZE_NOGC(x)
Is there a way to implement the IS_A_GC_POINTER macro above?
#define ARRAY_SIZE_NOGC(x) (sizeof(x) / sizeof(x[0])
This works with unmanaged array types, but not with pointers, since the size of a pointer is always the same and not the size of the data it points to. Obviously this doesn't work with managed types, which are all refered to by pointers. I could define the macro to handle managed arrays as follows
#define ARRAY_SIZE_GC(x) (x->Length
The problem is these two macros arn't generic. I would like one macro that would work for both. Something like the following
#define ARRAY_SIZE(x) ( IS_A_GC_POINTER(x) ? ARRAY_SIZE_GC(x) : ARRAY_SIZE_NOGC(x)
Is there a way to implement the IS_A_GC_POINTER macro above?