How to pass structure to function to allocate memory

时间:2016-07-11 19:45:41

标签: c function struct

As the title says i want to pass structure to function and allocate memory, maybe it's a stupid question but i can't find the answer..

structName *allocate_memory( int *numPlayers,structName )
{
   structName *retVal = malloc( sizeof(struct structName) * (*numPlayers) );

   return retVal;
}

The problem is in parameters structName what should go there? if you need the full code i can post it but i think there is no need..

4 个答案:

答案 0 :(得分:2)

You can't pass in a type as a parameter. But you can pass in its size:

void *allocate_memory( int *numPlayers, size_t struct_size)
{
   void *retVal = malloc( struct_size * (*numPlayers) );

   if (!retVal) {
       perror("malloc failed!");
       exit(1);
   }
   return retVal;
}

Then call it like this:

struct mystruct *s = allocate_memory(&numPlayers, sizeof(struct mystruct));

Or you just do this instead, assuming you want the memory initialized to all 0:

struct mystruct *s = calloc(numPlayers, sizeof(struct mystruct));

答案 1 :(得分:1)

You can use a void pointer there, void can take anything...hope it helps....

答案 2 :(得分:0)

You have two options, the first returning a new pointer (see allocate_memory) and the second is to fill in an existing pointer (see allocate_memory2. In both cases I converted numPlayers to int because it isn't necessary to provide by reference

struct structName *allocate_memory(int numPlayers)
{
    struct structName *retVal = malloc(sizeof(struct structName) * numPlayers);
    return retVal;
}

void allocate_memory2(struct structName **target, int numPlayers)
{
    *target = malloc(sizeof(struct structName) * numPlayers);  
}

int main(int argc, char** argv)
{
    struct structName *str;
    struct structName *str2;
    //After this line str is a valid pointer of size 20*sizeof(struct structName)
    str = allocate_memory(20);
    //After this line str2 is a valid pointer of size 20*sizeof(struct structName)
    allocate_memory2(&str2, 20);
}

答案 3 :(得分:0)

You cannot pass a type as a parameter to a function.

You basically have two options realizing your allocate_memory function:

  • Instead of passing the name of the type simply pass the size of the type: void *allocate_memory( int *numPlayers, size_t size). But this is only a trivial wrapper for malloc.
  • You could write a macro #define allocate_memory(num, type) (malloc(num * sizeof(type))) to do the job.

Maybe you're looking for a combination of both if you want to track some statistics of the memory allocated or do additional checks:

#define allocate_memory(num, type)   (my_malloc((num), sizeof((type))))

void *my_malloc(int num, size_t size)
{
    void *pMem = malloc(num * size);

    if (pMem == NULL)
    {
        /* do error handling */
    }

    return (pMem);
}

You can use the above macro as follows:

pInt = allocate_memory(5, int);   // allocates 5 integers
pStruct = allocate_memory(10, some_struct);   // allocates 10 some_structs