Search code examples
c++memorystack-overflow

Don't know why I am getting a stack overflow


I don't understand why I am getting a stack overflow immediately when I enter the main function. I am supposed to read from a text file and do some processing. Can someone explain to me the reason and suggest how to solve it?

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <fstream>
#include <iomanip>

using namespace std;
const int MAX=100;
enum countrytype{S,F};

struct dob
{
int day;
int month;
int year;
};

struct Local
{
char country[MAX];
char gender[MAX];
char name[MAX];
dob birthday;
int noofmod;
char mod[MAX][MAX];
int mark[MAX];

};

struct Foreign
{
char country[MAX];
char gender[MAX];
char name[MAX];
dob birthday;
int noofmod;
char mod[MAX][MAX];
int mark[MAX];

};

union Student
{
Local localstudent;
Foreign foreignstudent;

};

struct UOWstudent
{
countrytype ct;
Student st;

};

void readfile(ifstream &read,UOWstudent noofstudent[MAX]);

int main()
{
UOWstudent noofstudent[MAX];
ifstream read;

readfile(read,noofstudent);
cout<<endl
    <<noofstudent[0].st.foreignstudent.country
    <<endl
    <<noofstudent[0].st.foreignstudent.gender
    <<endl
    <<noofstudent[0].st.foreignstudent.name;



system("PAUSE");

}

void readfile(ifstream &read, UOWstudent noofstudent[MAX])
{
int i=0;
char country;
char filename[MAX];
cin>>filename;
read.open(filename);




    read>>country;
    /*if (country =='F')
    {
        read.getline(noofstudent[i].st.foreignstudent.country,MAX);

        read>>noofstudent[i].st.foreignstudent.gender;
        read.getline(noofstudent[i].st.foreignstudent.name,MAX);

    }

    else
        read.getline(noofstudent[i].st.foreignstudent.country,MAX);*/




}

This is my text file

F South Korea
Male Psy Park Jae Sang
31 - 12 -1977
3 CSCI114 55 CSCI103 44 GangNam

Solution

  • Simply, your code is allocating all of its storage on the stack, and you are allocating more than the allowed limit.

    Looking at why you are surpassing the limit is probably more useful.

    The first line of main() is allocating an array of 100 (MAX = 100) students on the stack:

    UOWstudent noofstudent[MAX];
    

    How big is a UOWstudent? You can figure that out by looking at each field:

    struct UOWstudent
    {
        countrytype ct; // enum. let's assume 4 bytes. (32-bit executable)
        Student st;     // ???
    };
    

    How big is a student?

    union Student
    {
        Local localstudent;
        Foreign foreignstudent;
    };
    

    It's the size of a Local or a Foreign, so let's just look at one. We need to make another assumption about the size of char. Let's assume 1 byte (8-bit characters):

    struct Local
    {
        char country[MAX];  // 100 bytes
        char gender[MAX];   // 100 bytes
        char name[MAX];     // 100 bytes
        dob birthday;       // 3 ints or 12 bytes (32-bit assumption again)
        int noofmod;        // 4 bytes
        char mod[MAX][MAX]; // 10,000 bytes
        int mark[MAX];      // 400 bytes
    };                      // total: 10,716 bytes
    

    So that very first line of main() tries to allocate (10,716 + 4) x 100 = 1,072,000 bytes on the stack. And I made the most conservative assumptions about the size of char and int for your compiler settings, they may quite possibly be higher. If the stack limit is indeed one megabyte (1,048,576 bytes), then this initial allocation goes over the limit.

    You can use C's sizeof operator to get information about the actual size of your types. Refer to this stackoverflow answer, discussing allocating arrays on the heap, instead of the stack, which is a good step towards solving your problem. (UOWstudent == University of Waterloo student?)