[ACCEPTED]-Allocate struct from function in C-struct

Accepted answer
Score: 10

The problem with your current code is that 11 the struct your creating is created on the 10 stack and will be cleaned up as soon as 9 the function returns.

struct foo
    int a;
    int b;

struct foo* create_foo( int a, int b )
    struct foo* newFoo = (struct foo*)malloc( sizeof( struct foo ) );
    if( newFoo )
        newFoo->a = a;
        newFoo->b = b;
    return newFoo;

This will get you a 8 heap allocated object. Of course, you'll 7 need a function to free that memory or this 6 is a memory leak.

void destroy_foo( struct foo* obj )
    if( obj )
        free( obj );

void print_foo( struct foo* obj )
    if( obj )
        printf("foo->a = %d\n",obj->a);
        printf("foo->b = %d\n",obj->b);

(btw, this style gets you 5 part of the way toward an "object oriented" C. Add 4 some function pointers to the struct (to 3 get polymorphic behavior) and you have something 2 interesting; though I'd argue for C++ at 1 that point.)

Score: 5

You have to return a pointer allocated via 6 malloc:

Employee* new_employee(char *_name, int birth_year, int start_year) {
    struct Employee* ret = (struct Employee*)malloc(sizeof(struct Employee));
    ret->name = _name;
    ret->birth_year = birth_year;
    ret->start_year = start_year;
    return ret;

two more things: (1) you should 5 make the struct definition of name a char* instead 4 of char[NAME_SIZE]. Allocating a char array makes the struct 3 much bigger and less flexible. All you really 2 need is a char* anyway. And (2) change the function 1 definition to char*.

Score: 1
Employee * make_employee(char *_name, int birth_year, int start_year)
    Employee *employee;

    if (employee = (struct Employee *)memalloc(sizeof(Employee)) == NULL)
        return NULL;
        strcpy(&(employee->name), _name);
        employee->birthyear = birth_year;
        employee->startyear = start_year;
        return employee;


Score: 1
  1. Why does the make Employee return void? You 15 need to return the Employee from the make_employee 14 function!

  2. Are you having trouble with the 13 compiler complaining about the x = {a,...} syntax? Write 12 it the long way then: Emp e; e.field1 = a; ...

  3. Are you having weird 11 overwriting / bogus numbers problems? If 10 you allocate a struct in the function it 9 will become invalid (and prone to being 8 overwriten) as soon as the function returns! To 7 go around this you either have to:

    • Return 6 a copy of the struct (this is OK for small 5 structs):

      Employee make_emp(int a){
          Emp emp; //Allocate temporary struct
          emp.filed1 = a; //Initialize fields;
          return emp; // Return a copy
    • Allocate the struct in the heap 4 instead and deal with it through references 3 (ie.: pointers) instead:

       Employee* make_emp(int a){
          Emp* emp = malloc(sizeof(Emp)); //Allocate the struct on the heap
                                          //And get a reference to it
          emp->filed1 = a; //Initialize it
          return emp; //Return the reference

      Don't forget to 2 free() the Employee after you are done with it 1 in this case!

More Related questions