1

I created a structure in a header file to store student information. I initialize these member variable in a function called createStudent(). The createStudent() function returns a pointer to a newly initialized student. Now, when I test that this all works I get some unexpected results.

The code compiles and runs, however, only the first access of the students information returns useful values. In the code below I print out the students age first and I get the correct age, but the gpa and num values are garbage values. However, if I comment out the print statement for the students age, the gpa value is correct and the num value is still garbage. It seems that only the first access returns goof values. Now I'm fairly certain this is an issue to do with my pointers and memory allocation, I just don't know what the problem is.

I initially thought that memory was being freed up after the first access (I don't know why this would happen but that what seemed to be happening). So I tried to use malloc in order to make sure there was memory available at all times storing the students info, but the results did not change.

I have also tried re-assigning age in the createStudent() function after gpa and num were assigned, but age still gets the corrected value and gpa and num have garbage values.

If you guys need any more information concerning this please let me know.

Header File:

    typedef struct Students{

         int num;                //holds the students number
         char name[256];         //holds the students name
         int age;                //holds the students age
         int gpa;                //holds the students grade point  average

    }Student;

    Student * createStudent();

Implementation file:

    Student * createStudent(){
         Student newStud;
         Student *studPtr;

         newStud.age = 10;       
         newStud.gpa = 5;        
         newStud.num = 307234;   

         studPtr = &newStud;

         return studPtr;
    }

Main File:

    int main(int argc, char *argv[]){
         int retCode = 0;

         Student *stdPtr = createStudent();

         printf("The students age is: %d\n", stdPtr->age);
         printf("The students gpa is: %d\n", stdPtr->gpa);
         printf("The students number is: %d\n", stdPtr->num);

         return retCode;
    }
Unks
  • 128
  • 7
  • Do you get any compiler warnings? – Moritz Schmidt Apr 25 '19 at 19:42
  • 5
    Your `createStudent` function won't work: you're returning a pointer to an object that exists on the stack. Use `malloc` or `calloc` to allocate on the heap for long-lived objects, or preallocate `newStud` in the calling function if the lifetime of the object won't exceed that function. – Dai Apr 25 '19 at 19:44
  • 1
    Okay. Thanks. I'll give that a try. Also I did not get any compiler warnings @MoritzSchmidt – Unks Apr 25 '19 at 20:16

1 Answers1

3

As mentioned in the comments, the allocated memory for Student newStud only lives as long as your function is executed. as soon as it returns, the memory is freed again.

If you want to return the address of a created structure, you have to allocate the memory by yourself:

Student * createStudent(){
     Student *studPtr = malloc(sizeof(Student));

     studPtr->age = 10;
     studPtr->gpa = 5;
     studPtr->num = 307234;

     return studPtr;
}

Do not forget to free the allocated memory and check the return value of malloc

Moritz Schmidt
  • 2,635
  • 3
  • 27
  • 51