Macro question...

I have a macro which expands an array for me. Anyway, one of the parameters is “type”, ie I pass what kind of array to expand. Anyway, I can’t pass anything like “char” or “int”. However, I CAN typedef a char, then use that. Am I doing anything wrong? Or should I be doing something else? Help! Thanks.

Could you post the macro definition? I’ve done similiar things in the past without any problem. However I will note that I wouldn’t do anything like that again. It really obfuscates the code I think.

It’s real simple…

#define ExpandSArray( array, count, type ) (array = (type *)realloc( array, sizeof array + (count * sizeof type) ))

try:
#define ExpandSArray( array, count, type ) {array = (type *)realloc( array, sizeof array + (count * sizeof type) )}

You might need the {'s.
Also, since array is a pointer, it has a size of 4 bytes, so your new array size is countsizeof(type)+4, not countsizeof(type)+oldsize.

Originally posted by Wulf:
try:
#define ExpandSArray( array, count, type ) {array = (type )realloc( array, sizeof array + (count * sizeof type) )}
You might need the {'s.
Also, since array is a pointer, it has a size of 4 bytes, so your new array size is count
sizeof(type)+4, not count*sizeof(type)+oldsize.

Wulf, You shouldnt need the braces because there is no need for a local namespace and sizeof(array) will return the number of elements in the array. So in both instances you are misleading…

Originally posted by 2:
#define ExpandSArray( array, count, type ) (array = (type *)realloc( array, sizeof array + (count * sizeof type) ))

In the above code fragment you are missing ()'s around the arguments of two of the sizeof operations (specifically ‘sizeof array’ and ‘sizeof type’ should be ‘sizeof(array)’ and ‘sizeof(type)’ respectively).

When I made those changes and compiled the test code it worked like a charm.

Here is the test code:

#define ExpandSArray( array, count, type ) (array = (type *)realloc( array, sizeof( array) + (count * sizeof( type)) ))

// return true on success
bool AppInit(Window* pWindow)
{
int* a;

// create array with 4 elements 
a = (int*)malloc( 4* sizeof(int));

// add 4 more elements to array
ExpandSArray( a, 4, int );

}

Yeah, I know that wont compile by itself, just make a new function header .

[This message has been edited by BwB (edited 03-31-2001).]

… because this just illumiates problems with opengl SO well.

Thanks everyone who responded. Sorry for the C++ question, but it’s for an OpenGL program, so I figured that I would just post it here. Plus I know that this community knows a bunch about C++ so I would get good answers (and I did). Thanks!

Originally posted by BwB:
sizeof(array) will return the number of elements in the array. So in both instances you are misleading…

No, sizeof on a pointer returns the size, not the size or # of elements in its array. The only time it’ll return the # of elements is when used on a fixed-size array, at least on my compiler because sizeof must be a constant (msvc6).

#include “stdio.h”
#include “malloc.h”

#define ExpandSArray( array, count, type ) { array = (type )realloc( array, sizeof( array) + (count * sizeof(type))); printf("Old array size: %d
New array size: %d (%d)
",sizeof(array),sizeof(array)+count
sizeof(type),count*sizeof(type)+4); }

void main()
{
int a;
a=(int
)malloc(sizeof(int)*16);
ExpandSArray(a,32,int);
}

The output should look like:
Old array size: 4
New array size: 132 (132)

My apologies Wulf, the proper macro should look like this:

#define ExpandSArray( array, count, type ) (array = (type *)realloc( array, (sizeof(array) / sizeof(type)) + (count * sizeof(type)) ))

[This message has been edited by BwB (edited 04-02-2001).]

it’s also a good idea to put () around the variables in the macro definition…you might get funny errors w/o them

b