Stack Memory Overflow
Suppose we have a program in which we didn't dynamically allocates memory on heap but using stack and for some reason the size of data (variables) are in high numbers or of more Mbs than our memory and our program surely crash For examples I have 4 classes each class has its own 30 variables and all objects of these classes are in used at the same time so did our stack memory is able to deal with this? Did we call this problem stack memory overflow? As we know stacks are more efficient than heap so can we solve this problem without using heap? Or we have to remove some variable from the classes to avoid memory overflow? Two months ago I make a program in which my program get crash some where while running so is this a problem of stack memory overflow https://code.sololearn.com/ce7VLY70NMEu/?ref=app
9/9/2020 1:28:04 PMSaad Mughal
9 AnswersNew Answer
It is not crashing at sololearn and offline mobile compiler for me. but here is a potential source of crash Lecture l; size is 20 (last valid index 19) but you are assigning 21 elements (accessing l which is invalid index)
This is not directly related to question but I'll suggest not to use c-style char array to store strings. C++ has better alternative, std::string. you are declararing many char 's like: char sub1; char sub2; . . char sub12; You can use an array of std::string's instead: std::string sub;
Memory is not the issue here. You are hardly using 1.2-1.5 MB memory (just total up the size of arrays plus memory used for internal bookkeeping) Is your application crashing? Also in StoreData method make all the parameters const to avoid compiler warning. Can you tell for which input your code is crashing?
~ swim ~ thanks bro actually that's the actual problem I am assigning 21 elements to 20 objects array As I am not expert so it's my mistake I change the array size to 21 now I am not getting error "program has stop working" on Dev I think that the thing where I overflowed the stack array Unfortunately mobile compiler not giving this warning But the problem resolve successfully thank you
Saad Mughal Yes it was a case of buffer overrun which leads to undefined behavior including crash. The crash may not happen immediately but it will definately create problems later on. Glad to know your issue is resolved.
~ swim ~ Ipang CarrieForle
🇮🇳Omkar🕉 OK thanks I also used string instead of char in this but problem remain the same as size of string is I think more than char
~ swim ~ https://code.sololearn.com/c5ZAHmo53iHp/?ref=app Try to enter choise 2 after entering the subject the name It says your program has stop working on my Dev C++ compiler
~ swim ~ 😊