So I'm a high school student learning C++. My project is to make a basic-terminal based inventory system for the Chemistry lab. Since we haven't reached Data Handling I was trying a prototype for the code. In DevC++ the compiled code
just flat out crashed the terminal prompt and in Visual Studio It opened another tab showed a bunch of code I couldn't understand and gave me something called a "Stack Overflow". I'm posting the code here. Please help me.
programs us a stack structure to store local variables and function calls and internal workings. If you exceed its capacity, which takes a lot of data, you can get a stack overflow. I don't see why here, yet, and I have to run, but you have something that is too large to be static data.
One stock object contains:
2 int
100 char (no_containers)
100 char (name)
40 char (chem_formula)
100 exp_no, but each exp_no has 100 char, i.e. 10'000 char
In short: 10'240 char + 2 int
The array of thousand thus requires stack memory for:
10'240'000 char + 2'000 int
An array of char is more difficult to keep safe. It has fixed size, but user input might be more or less than that. std::string is easier and safer, and does not store the text in stack (except as optimization that you don't have to worry about).
std::vector is a more generic dynamic array type that also stores data outside of stack.