[Dev-C++] Crash with macro to template conversion
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: David M. <ci...@ya...> - 2004-02-27 14:45:41
|
Ok here is my problem.
I want to replace:
#define CREATE(result, type, number) \
do \
{ \
if (!((result) = (type *) calloc ((number), sizeof(type)))) \
{ \
perror("malloc failure"); \
fprintf(stderr, "Malloc failure @ %s:%d\n", __FILE__, __LINE__ );
\
abort(); \
} \
} while(0)
With it's C++ equivalent.
So far I have:
#define CREATE(result, type, num) = create<type>((result),(num))
template<class T>
void create(T * ptr, int number, char * f_name = __FILE__, int l_num
=
__LINE__)
{
try {
ptr = new T[number];
}
catch(bad_alloc& b)
{
perror("malloc failure");
fprintf( stderr, "Malloc failure @ %s:%d\n", f_name, l_num );
abort();
}
if ( !ptr )
{
perror("malloc failure");
fprintf( stderr, "Malloc failure @ %s:%d\n", f_name, l_num );
abort();
}
return ptr;
}
But the program crashes after the function call when any attempt to
acces
the data occours:
e.g.
OBJ_DATA * obj;
CREATE( obj , OBJ_DATA, 1 );
obj->some_field = 5; //Crashes here and obj = 0x0 from gdb.
Thanks in Advance
David McKen
__________________________________
Do you Yahoo!?
Get better spam protection with Yahoo! Mail.
http://antispam.yahoo.com/tools
|