This is the case. Teacher asked the task. In which it is necessary - to allocate memory for the elements of the array and then free up memory. Made a task.

typedef struct { int Id; char FIO[NAME_SIZE]; int Mark; char Teacher[NAME_SIZE]; }Student; Student students[100]; 

I think that the last line I allocate memory for 100 items. But! I began to read about the sequence, found some:

Init is the initial memory allocation for the sequence elements;

And here is the question. Even two. Do I need to do a memory allocation through init (the teacher is on vacation, so it's too late to rush around, if doing it right through init means you have to do it)? And the second is where to read about it. Slightly google operation init, about it except in my training manual - there is nothing. Help plz!

Refinement describes the Init function:

 int Init(SEQ* seq, int MaxSize) { if (seq->pArr != NULL) return -1; seq->pArr = (int*) malloc(MaxSize*sizeof(int)); if (seq->pArr == NULL) return -2; seq->nMaxSize = MaxSize; seq->nSize = 0; return MaxSize; } 
  • Sequences? What would it mean? What sequences are we talking about and what is Init? - cy6erGn0m
  • "To implement a dynamic array, which is also called a sequence in the literature, it is necessary to ensure the implementation of the operations of adding and deleting its individual elements." - Aleksey Makas


2 answers 2

There are no init operations in C / C ++. As well as such functions. Perhaps it was implied that in the case of writing your own classes, you need to separately construct an object, and separately call the function defining its initial state. But then she can have any reasonable name, as long as there is clear documentation on how to use this class, but I consider this an unfortunate decision, since Usually in the constructor you can immediately make the object complete and ready for use.

If we are talking about assigning and deleting memory, we should pay attention to working with dynamic memory (heap or a heap). In C, a couple of malloc / free functions are used for this, in C ++ it is better to use the operators new and delete. And then it turns out that in the manual they want you to just break your program into functions, one of which will create your array, and the other to delete. Well, this is in addition to the work itself =)

  • Maybe you are right. In the manual, the operation Init was specified, as if this word was already reserved ... And then there was a code with an example, it first declared the Init () function, and then described such a thing (see the question). I am not strong in codes. It turns out Init is a user-defined function? Word not reserved? - Aleksey Makas
  • Began to read) Oh, yes! This is what the lists need! Thanks!) Only this does not seem to solve the issue of memory allocation and freeing memory. - Aleksey Makas

I can assume that you need to implement the list (based on the personal experience of the student). If so, you here

  • Yes, the lists also sounded somewhere, the question is that if you can implement without them, if you are interested in me, if you are not interested in the language, then can you do everything without them? In the task about the lists nothing is said, in the comments to the labs there is also silence about the lists. Clarified, even in cr it is not specified what to do with lists ... Sorts, two-dimensional arrays, user-defined functions ... Nothing about lists. Can you live without them? - Aleksey Makas
  • one
    student students [100], in this case students [i] is an element of the list - Egor Sokolov
  • got it! just realized) If in the comments, say that Student students [100] is the memory allocation, and write how to clean it (memory) - then close the question! - Aleksey Makas
  • one
    read carefully the second part of the gecube answer, in my opinion exploring) - Yegor Sokolov