"Access Violation (Segmentation Fault)" error in Program

Hi, I'm not that new to programming, but I'm not a veteran either.

First the specs.

OS: Windows 7 Home Premium 64-bit
Compiler: Dev C++ ver 4.9.9.2

Problem: In brief, I'm making a Maze-type game on the console. It's a project that contains 5 files: 3 sources, 2 headers.

Here's the main.cpp of my project:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <fstream>
#include <vector>
#include <conio.h>
#include <time.h>
#include "_functions.h"
#include "_classes.h"

using namespace std;

main()
{
      room Room0("resources/maps/map0/room 0.txt");
      getch();
}


When I try to run the program, it immediately crashes. So I poked around with the debugger and found the following:

Here's the "room" class first of all (header file):
1
2
3
4
5
6
7
8
9
class room
{
      public:
             room(string file);
      
      private:
              string name, directions, event[];
              char directionbuf[];
};


Error occurs in the constructor of the "room" class (source file):
1
2
3
4
room::room(string file)
{
            readFile(file, DISPLAY);
}


("DISPLAY" is an int defined in the preprocessor directives earlier in the same file)

The "readFile" function (source file):
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
string readFile(string filename, int control)
{
       ifstream infile(filename.c_str());
       string line, result;
       if (infile.bad()) control=0x0;
       
       switch (control)
       {
              case 0xA00:
                   while (infile.good())
                   {
                         getline(infile, line);
                         result+=(line+"#");
                   }
                   break;
              
              case 0xA01:
                   while (infile.good())
                   {
                         getline(infile, line);
                         cout << line << endl;
                   }
                   break;
              
              default:
                      cout << "Could not open file: \"" << filename << "\"";
       }
       
       infile.close();
}


The function runs through fine, displaying the contents of the .txt file, but once the function is finished executing, the debugger spews an error saying:
Warning: An Access Violation (Segmentation Fault) raised in your program.


I've done a little research as to what the error was, but still don't have a good understanding as to what the error is. The code compiles fine, so I assume it's my methods of approach that are causing the error.


This is my first post on the cplusplus forums. I'm in need of constructive criticism to help me out on my post/future posts. I appreciate any help, thanks :)
1
2
3
      private:
              string name, directions, event[];
              char directionbuf[];


Ummm... that's not legal. You can't have arrays without a specified size. This should not even be compiling.
Last edited on
1
2
3
4
5
6
7
8
9
class room
{
      public:
             room(string file);
      
      private:
              string name, directions, event[];
              char directionbuf[];
};


This is illegal in C++. These are arrays with 0 elements and accessing any of the (non-existent) elements therein will mean you're accessing memory you don't own.

You might want to disable whatever compiler extension allows that.
Your function returns a string, but where do you return it?
Totally missed that thanks, but i'm still getting the same error, on the same line as well.
Post your updated code (only the functions and classes you changed)
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
class maphandler
{
      public:
             friend class room;
             
             void eventBNS(room bonusroom);
             void eventDTH(room startroom);
             void eventWRP(room targetroom);
             void eventWIN();
             void ApplyEvent(room eventroom);
      
      private:
              vector<room> Rooms;
              string eventbin[5];
};

class room
{
      public:
             room(string file);
      
      private:
              string name, directions, event[5];
              char directionbuf[4];
};


Changed the header file with the class declarations, nothing else.

*edit*: L B, you're right about my "readFile" Function not returning anything. I tried the code out and it works fine. Thanks
Last edited on
So, you didn't change your readFile function despite my warning?
just did before you started to post :)
Last edited on
Topic archived. No new replies allowed.