简体   繁体   中英

can i pass a variable type as a paramter in a function in C language?

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM