2

I have a question about structures, as I was programming some code and after I added a structure the program always crash, so I isolated a small part of it and I found that struct STUDENT is the cause of it.

when I declare the array of STUDENT students [MAX] in main the program crashes and I have no idea why, the program only runs when I

1.Change subjects2 in STUDENT to not a array. However I do need to store more than one subject that belongs to a student

2.Declare STUDENT in main not as a array. I need an array as I have to store possibly a large amount of students.

Is there possibly somewhere wrong with my declaration? I kindly ask for some help, thank you in advance.

#include<iostream>


using namespace std;
const int MAX = 100;
enum Grade {HDist, Dist, Credit, Pass, Fail};
struct assessment_Task
{
char Title_Name[MAX];
int Weight;
int Mark;

double A_Mark;

};



struct SUBJECT
{
  char subject_Code[MAX];
  char subject_Title[MAX];
  int No_Assess_Task;
  assessment_Task AT [MAX];
  int finalMark;
  Grade grade;
};

struct STUDENT
{
  char Name[MAX];
  int ID;
  char Subjects_Taken[2][50];
  SUBJECT subjects2 [MAX];

};

int main()
{

STUDENT students[MAX];




}
eatmybananapls
  • 103
  • 1
  • 9
  • 2
    Try to find out the size of each involved type using the `sizeof` operator, like e.g. `cout << "SUBJECT: " << sizeof SUBJECT << endl;`. It could be that MAX students with MAX subjects each containing MAX assessment tasks simply doesn't fit into memory. Fixes: Firstly, replace `char[MAX]` with `std::string`. Then, replace other raw arrays with `std::vector`. – Ulrich Eckhardt Jan 14 '18 at 08:50

2 Answers2

4

As John3136 has answered, you're likely blowing up the stack that caused the crash.

I believe you can't well utilize that much elements in each array, and since you're using C++, I recommend using STL containers that helps you manage memory better.

First, replace all your char[] arrays to std::string. It has many handy features in addition to memory management. Even if you really need a C-style string, you can call str.c_str() to have one.

Then, replace all those arrays with std::vector. This is the well-known dynamic array container in C++. It uses dynamic memory allocation to arrange the array and won't blow up the stack like your current code is doing.

iBug
  • 35,554
  • 7
  • 89
  • 134
  • Yes! Exactly ! I was just typing a comment on John's answer when you answered this. I know that the term is still often used, but note that there is no STL (=Standard Template Library) anymore because it was integrated into the standard library a long time ago. Standard containers will do ;-) – Christophe Jan 14 '18 at 09:26
3

Doing some rough calculations based on 32 bits ints, no padding etc - basically some simple assumptions

  • assessment_Task is about 112 bytes
  • Subject is about 11412 bytes
  • Student is about 1141404 bytes (over 1 meg)
  • Therefore your of 100 students is over 100 meg

So that's over 100 Meg you are trying to put on the stack. Depending on the OS, your stack size is probably up to 8 meg or so (without special compiler optiions to set it). To confirm, try making the array of students a smaller size (e.g. 1)

Basically you need to reduce MAX or use a different MAX for different parts to reduce the size. Obviously the best solution is using vectors or similar and dynamically allocating the structs.

See Is there a limit of stack size of a process in linux and C/C++ maximum stack size of program for more info on stack size.

John3136
  • 28,809
  • 4
  • 51
  • 69
  • Yes ! Using std::string instead of char arrays and std::vector instead of other arrays would solve the problem: those datastructures have a minimal footprint on the stack and manage the heavy part of the memory elsewhere. – Christophe Jan 14 '18 at 09:18
  • Thank you so much, as I'm still fairly new to c++ I do not know much about the size and memory, but due to this assignment I'm definitely learning about size, memory and it's effect. – eatmybananapls Jan 14 '18 at 12:57