0

I am a beginner in C and I'm trying to build my own student database. Here I'm trying to create a new student and I'm trying to understand how to use heap declarations. Here is code that I have using a stack declaration:



student* create_student(char *given_name, char *family_name, int age,
        char* gender, int *promotion)
{

    student s;
    s.given_name = given_name;
    s.family_name = family_name;
    s.age = age;
    strncpy(s.gender, gender, strlen(gender)+1);
    s.promotion = promotion;
    puts("---print inside create_student function---");
    print_student(s);
    puts("---end of print inside");
    return &s;
}

I understand that since we are using a stack here, the information is lost outside of the function, however I'm a bit confused as to how I can "convert" this into a heap.

[I have studied C++ before so I tried with something like student *s = new student]

So my question is, how would I convert this into a heap declaration so as to retain the info outside of the function?

trincot
  • 317,000
  • 35
  • 244
  • 286
  • If you use something like `new student` you must be using C++, not C. – Gerhardh Apr 20 '20 at 10:26
  • You assign some pointers while you copy strings for others. How is your interface defined? Where is the memory for the strings taken from? Who is responsible for allocating and freeing it? Also. `strncpy(..strlen(gender)+1)` does not make any sense. It breaks the intent of `strncpy`. – Gerhardh Apr 20 '20 at 10:27
  • Ah okay I did C++ before, I assumed you could use the same in C! I found something like malloc but could really understand it –  Apr 20 '20 at 10:27
  • Does this answer your question? [Return address of local variable in C](https://stackoverflow.com/questions/8743411/return-address-of-local-variable-in-c) – Gerhardh Apr 20 '20 at 10:30
  • ASIDE: Your `strncpy(s.gender, gender, strlen(gender)+1);` call is equivalent to `strcpy(s.gender, gender);` and provides no buffer overflow protection at all. You need to limit the copied amount to the size of the destination buffer, and also note that `strncpy` does not put a null terminator at the end of the destination buffer if the source string is at least as long as a destination buffer, so extra code is needed to ensure the destination is null terminated. – Ian Abbott Apr 20 '20 at 10:31
  • Alright I see! I'll change it to strcpy, thanks! –  Apr 20 '20 at 10:33
  • @Gerhardh hi! sorry for getting back to you so late, but your link didn't help out with what I wanted. Thank you in any case! it was helpful (: –  Apr 21 '20 at 23:11

1 Answers1

1

As you point out, if you keep student s in the stack, the memory block is freed once the function returns and you don't have access to that information anymore. Variables like this have what is known as automatic storage duration.

Using the heap allows you explicitly manage your memory and therefore allows to store information in a more flexible way as memory is allocated and deallocated on request. Variables like this have what is known as allocated storage duration.

Thus, to store your information in the heap you use dynamic memory allocation functions like malloc() or calloc().

In your case, to use the heap, we would do:

student* create_student(char *given_name, char *family_name, int age,
        char* gender, int *promotion)
{

    student *s = malloc(sizeof(student));
    s->given_name = given_name;
    s->family_name = family_name;
    s->age = age;
    strncpy(s->gender, gender, strlen(gender)+1);
    s->promotion = promotion;
    puts("---print inside create_student function---");
    print_student(s);
    puts("---end of print inside");
    return s;
}

When you no longer need the memory allocated in the heap you should free it by using free().

CRM
  • 4,569
  • 4
  • 27
  • 33