PDA

View Full Version : Macro question...



2
03-31-2001, 10:43 AM
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.

DFrey
03-31-2001, 01:26 PM
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.

2
03-31-2001, 03:43 PM
It's real simple...

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

Wulf
03-31-2001, 05:08 PM
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.

BwB
03-31-2001, 05:55 PM
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 http://www.opengl.org/discussion_boards/ubb/smile.gif.


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

john
03-31-2001, 07:57 PM
... because this just illumiates problems with opengl SO well.

2
04-02-2001, 08:55 AM
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!

Wulf
04-02-2001, 11:13 AM
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\nNew array size: %d (%d)\n",sizeof(array),sizeof(array)+count*sizeof(type),co unt*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)

BwB
04-02-2001, 11:19 AM
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).]

coredump
04-02-2001, 06:37 PM
it's also a good idea to put () around the variables in the macro definition...you might get funny errors w/o them

b