Stack Overflow?

For some reason I'm getting a stack overflow with this code. I've done a little C++ before in school but have just recently gotten back into it for my work. I've looked to see what the error might be but I can't see it...of course I might be missing something major since if never worked with strings to any great extent.

Can someone give me a direction on this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  #include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

//import all data for new and old alarm descriptions. set up some kind of if test to determine
//if the strings of data match or not.

	struct AlarmNames
	{
		std::string Equiptype;
		char Pound;
		char Prefix;
		char Dash;
		int Number;
		std::string Channel;
	};
	int main()
	{
		std::ofstream FILE("MEG BREAKERS TEST");
		std::ofstream  myfile("STUFFS.txt");
		std::ofstream myfile2("STUFFS2.txt");
		AlarmNames Equipment[22620];
		std::ifstream input1("Equipment names.txt");
		std::ifstream input2("Equipment Pound.txt");
		std::ifstream input3("Equipment prefix.txt");
		std::ifstream input4("Equipment Dash.txt");
		std::ifstream input5("Equipment Number.txt");
		std::ifstream input6("Equipment Channel.txt");
		

		for (int i = 0; i < 22620; i++)
		{
			input1 >> Equipment[i].Equiptype;
			input2 >> Equipment[i].Pound;
			input3 >> Equipment[i].Prefix;
			input4 >> Equipment[i].Dash;
			input5 >> Equipment[i].Number;
		}
		
		for (int i = 0; i < 22620; i++)
		{
			if ((Equipment[i].Equiptype == std::string("PCB")) ||
				(Equipment[i].Equiptype == std::string(" "))||
				(Equipment[i].Equiptype == std::string("pcb")))
			{
				FILE << "PCB " << std::endl;
			}
			else if ((Equipment[i].Equiptype == std::string("REG")) ||
				(Equipment[i].Equiptype == std::string("REGULATOR")) ||
				(Equipment[i].Equiptype == std::string("regulator")) ||
				(Equipment[i].Equiptype == std::string("reg")) ||
				(Equipment[i].Equiptype == std::string(" ")))
			{
				FILE << "REGULATOR " << std::endl;
			}
			else if ((Equipment[i].Equiptype == std::string("XFMR")) ||
				(Equipment[i].Equiptype == std::string("xfmr")) ||
				(Equipment[i].Equiptype == std::string("transformer"))||
				(Equipment[i].Equiptype == std::string("TRANSFORMER")))
			{
				FILE << "XFMR " << std::endl;
			}
			else
			{
				FILE << Equipment[i].Equiptype << std::endl;
			}
		}
		return 0;
	}
AlarmNames Equipment[22620];

That is a HUGE array you're declaring on the stack there.
Most likely, this is the cause (or at least the biggest contributing factor) to your stack overflow (since stack space is usually quite limited).

I would consider replacing it with a std::vector:
std::vector<AlarmNames> Equipment(22620);
(Remember to #include <vector> as well. You won't need to make any other changes to your code.)
Thank you,

I'll give it a shot. I had a hunch that might be the case, but I wasn't even close to being sure and I didn't want to change anything major in my code without some assurance.

*edited about five seconds later*

The code now compiles but doesn't work like I anticipated..i.e. no output. I'll play around with it to see what it could be. Meanwhile I need to learn more about #include <vector>

Thanks for your response.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <fstream>
#include <string>
#include <map>

int main()
{
    const char* name_file = "Equipment names.txt";
    const char* output_file = "MEG BREAKERS TEST.txt";
    
    std::ifstream in(name_file);

    if (!in.is_open())
    {
        std::cout << "Unable to open \"" << name_file << "\" for input.\n";
        return 0;
    }

    std::ofstream out(output_file);

    if (!out.is_open())
    {
        std::cout << "Unable to open \"" << output_file << "\" for output.\n";
        return 0;
    }

    std::map<std::string, std::string> sanitizedName =
    {
        { "pcb", "PCB" },
        { "REG", "REGULATOR" }, { "reg", "REGULATOR" }, { "regulator", "REGULATOR" },
        { "xfmr", "XFMR"}, { "transformer", "XFMR"}, { "TRANSFORMER", "XFMR" }
    };

    std::string token;
    while (in >> token)
    {
        auto it = sanitizedName.find(token);
        if (it != sanitizedName.end())
            token = it->second;
        out << token << '\n';
    }
}
Thanks......I never even thought of that!

works like a charm.

-Tyler.
Topic archived. No new replies allowed.