C++ Eclipse Question

JC724

Weaksauce
Joined
Jan 20, 2016
Messages
118
First off I am running my program in Eclipse on Virtual box in Linux.

My program crashes when I add an additional array. I have not even used this array. So I am wondering if I am using too much memory.

Here are my data structures I create at the top of the program.

//used in task 0
class CFG_Rules {

public:
string LHS;
string RHS[500];
int ruleNumber;
};

//used in task 1
class CFG_Rules1 {

public:
string LHS;
string RHS[500];
int numberOfRHSeachRule;
};
//used in task 1
class FIRST_SETS {

public:
string firstSetsNonTerminals;
vector<string> vFirst;
set<string> sFirst;
int numberOfSets;
};

//used in task 1
class CFG_Rules2 {

public:
string LHS;
string RHS[500];
int numberOfRHSeachRule;
};

//used in task 2
class FOLLOW_SETS {
public:
string followSetsNonTerminals;
set<string> sFollow;
int numberOfFollowSets;
};



I create some arrays in main here

//creating data structure of rules
CFG_Rules myRules[1000];
//creating data structure of rules for task1
CFG_Rules1 myRules1[1000];
//creating data structure of First Sets for task 1
FIRST_SETS firstSets[1000];
//creating data structure of rules for task2
FOLLOW_SETS followSets[1000];

The issue comes here

the entire program works.

But once I create this array
CFG_Rules2 myRules2[1000];

The program crashes when I run it.

I don't even use this array. When I remove it, the program works when I run it.

Any ideas of what is happening?? Am I running out of memory in Eclipse??
 
It's possible, but not unheard of. Why are you running eclipse in a linux VM?
 
It's possible you are running out of stack space. Really easy to do with large static arrays, lots of inner loops, and deep recursion.

Just to be safe, I'd suggest placing your large array data on the heap (i.e. malloc, new, etc.), or, alternatively, using an STL container such as std::array or std::vector.
 
std::array is still on the stack, it just has added functions for convenience and won't implicitly decay to a pointer, but is no different (in memory) than a c-style fixed size array.

You're putting too much data on the stack when you declare it inside of "main", so you're hitting a stack overflow as soon as the program runs.

What you can do if you don't want to allocate it on the heap is mark those arrays "static" (by putting "static" in front of their declarations). This will store them in a different piece of your application's memory. Your application size will grow, but you won't crash anymore. You can alternatively move them out of the function into global scope (anywhere outside of a function) and that will accomplish the same thing (stores the variables in static memory, or the ".data" segment of your program).

Static variable - Wikipedia, the free encyclopedia
Data segment - Wikipedia, the free encyclopedia
 
Back
Top