Segfault upon compilation

My last question was far too wordy and I tried using an entire program to describe my problem. I have since made my issue into 40 lines, namely because I fixed the last issue, and because this is easy to show in 40 lines.

The largescale problem keeps on segfaulting, specifically when current == 25. I don't think I am referencing any memory that doesn't exist, but somehow I keep on getting segfaults. This program in particular won't even compile, but yet my multi-thousand line program will at least make it to 24 before segfaulting at 25.

Where is my program referencing invalid memory?

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
#include <ncurses.h>

int movementReturn(int current);

int main()
{
    int area[5][5];
    for(int j = 0; j < 5; j++)
    {
        for(int k = 0; k < 5; k++) { area[j][k] = (k+1)+(j*5); }
    }
    int current = 1;
    while(TRUE)
    {
        current += movementReturn(current);
        printw("\n%d",current);
        printw("\n");
    }
}

int movementReturn(int currentPos)
{
    if((currentPos + 5) < 26) { printw("Type 1 to move downwards.\n"); }
    if((currentPos - 5) > 0) { printw("Type 2 to move upwards.\n"); } //these work this way because I was a moron and programmed one of the variables wrong.  
    if(currentPos % 5 != 0 and (currentPos + 1) < 27) { printw("Type 3 to move rightwards.\n"); } //basically up is down and down is up which is like
    if((currentPos - 1) % 5 != 0 and (currentPos - 1) > 0) { printw("Type 4 to move leftwards.\n"); } //unfortunate, but nonetheless it works and runs, and doesn't really need changing.  
    
    int moveInput = (getch() - '0');
    
    if(moveInput == 1 and ( (currentPos + 5) < 26) )     { return 5; } //we need to make the if statements stop the increase to stop segfaulting in case the user types something they shouldn't.  
    else if(moveInput == 2 and ( (currentPos - 5) > 0) ) { return -5; }
    else if(currentPos % 5 != 0 and moveInput == 3 and ( (currentPos + 1) < 26) ){ return 1; }  
    else if((currentPos - 1) % 5 != 0 and moveInput == 4 and ( (currentPos - 1) > 0) ) { return -1; }  //now the player can't move back once they reach an area divisible by 8.  
    
    else { printw("You can't go that way\n"); return 0; } 
}
Because I don't have ncurses, I changed your printw calls to printf calls, and changed the getch call to just be scanf (#included <cstdio> or <stdio.h>).

I don't see any particular place in your program that would segfault. Some confusing if-statement logic, but no segfaulting. Note that your area array is never used for anything useful (only assigned to).

If you can't get your code to compile, I suggest posting the compilation/linker errors.
You're missing "initscr()" to initialize curses mode.
And you should say "endwin()" at the end to terminate it.
You need a tutorial. http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/
Last edited on
No, I don't need a tutorial, I just copy pasted bits from my actual code and must have forgotten the other bits.

After adding in initscr and endwin, my program runs fine, but somehow segfaults when the current variable reaches 25 on the larger program.

https://onlinegdb.com/r1iKTCPeV

This link will probably be infinitely more understandable than me trying to put the code into smaller files.

When debugging I found that this may be important:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    std::string areaDesc[25];
    std::fstream areaStream; areaStream.open("areas.txt");
    
    for(int i = 0; i < 25; i++) { getline(areaStream,areaDesc[i]); }

    bool playing = TRUE;
    int area[5][5];
    int current = 25;
    for(int j = 0; j < 5; j++)
    {
        for(int k = 0; k < 5; k++) { area[j][k] = (k+1)+(j*5); }
    }

        std::string str = areaDesc[current]; 
        for(int i = 0; i < str.length(); i++) { addch(str[i]); }



Immediately after selecting the class on the shared link, the program segfaults.
Last edited on
No, I don't need a tutorial

F you , then. :-)
That's not helpful. I'm just trying to understand what's going on, and I feel it is best you also understand what is going on.

Edit:
To what Ganado said, yeah, I am unsure of what is causing it to crash as well. I did find out it wasn't compilation time segfaulting, but rather when "current" reaches 25. Current was set to 25 by default when I tried debugging it on my own, but that has since been ineffective.

Last edited on
Yes, index 25 is out of bounds if the size of the array is 25.
Oh. Right, arrays start at 0. I guess that kind of flew to the back of my mind. I could spend the next ten minutes explaining myself but I'm sure you understand, thank you for pointing out my awfully obvious error.
Topic archived. No new replies allowed.