Reputation: 1
In C language: I have a function that allocate extra memory but i need to send the type of memory allocation i need like (int / char / any struct) as a parameter. I think writing 2 functions that do the same thing is not good.
Upvotes: 0
Views: 464
Reputation: 123578
You could create a macro to wrap around your function, something like
#define MY_ALLOC(type,count) my_alloc(sizeof(type), (count))
void *my_alloc( size_t size, int count )
{
return malloc( size * count );
}
and then call it as
void *int_memory = MY_ALLOC(int, 10); // expands to my_alloc(sizeof(int), 10);
void *dbl_memory = MY_ALLOC(double, 20); // expands to my_alloc(sizeof(double), 20);
You could also use the #
operator to "stringize" the type name and pass it to the function:
#define MY_ALLOC(type,size) my_alloc(#type, count)
void *my_alloc( const char *type, int count )
{
if ( !strcmp( type, "int" ) )
return malloc( sizeof(int) * count );
else if ( !strcmp( type, "double" ) )
return malloc( sizeof(double) * count );
...
}
MY_ALLOC( int, 10 ); // expands to my_alloc( "int", 10 );
MY_ALLOC( double, 20 ); // expands to my_alloc( "double", 20 );
This is not terribly robust or elegant, though.
I think writing 2 functions that do the same thing is not good.
Ultimately, if you have something that really has to be type-aware (which malloc
is not, it only cares about size), then you must have separate implementations for each type. There are tricks and tools you can use to hide it all behind a single interface (such as the macro trick above, or by using the _Generic
keyword) so your main application logic doesn't have to worry about it, but at some point you must have int
-specific code vs. double
-specific code, etc.
Upvotes: 0
Reputation: 181714
i need to send the type of memory allocation i need like (int / char / any struct) as a parameter.
No, function arguments must be expressions, which at runtime are evaluated to produce the actual values that are passed. Data types are not expressions.
You could create a way to represent specific data types of interest, such as an enum
, for example:
enum data_type { CHAR, SHORT, INT, LONG, LONGLONG, FLOAT, DOUBLE, /* ... */ };
Then you could pass appropriate ones of those values. But that covers only the specific data types that you provide for in advance.
But take a step back. You say you want to convey data type information in order to allocate memory, but what would your allocator actually do with the data type if you could convey it? Answer: determine its size. The standard library's memory allocation functions don't care about type, only size. So constructing a framework for expressing data type information to your allocator would be a baroque waste compared with just passing the size in the first place.
In fact, you should consider what you think you gain by writing a generic allocation function at all, as opposed to using malloc()
or calloc()
directly. A few valid reasons do exist, but if you can't name one then you're getting ahead of yourself.
Upvotes: 4