Urgent Help With A Segmentation Fault!

Like the title says, I'm wondering why my program is giving me a segmentation fault when I try to run it. For example, in the testequiv.cpp file I try to call the merge function and it triggers a segmentation fault. This also applies to the leader and equivalence function as well. I would greatly appreciate any feedback!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using namespace std;

#include "equiv.h"
#include <cstdio>
#include <iostream>

// newER(n) returns an array of n+1 integers.

ER newER (int n)
{
    ER boss = new int[n+1];

    int i = 1;

    while (i <= n)
    {
        boss[i] = i;
        i++;
    }

    return boss; 
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
// leader(boss, x) returns the leader of x in equivalence relation boss.

int leader(ER boss, int x)
{
    if (boss[x] == 0)
    {
        return x;
    }

    else
    {
        return leader(boss, boss[x]);
    }
}

Last edited on
What is your assignment about?

Anyways here is the output I got, the program freezes but no segmentation fault :
--- Current ER ---
At index 1 the array value is 1
At index 2 the array value is 2
At index 3 the array value is 3
At index 4 the array value is 4
At index 5 the array value is 5
At index 6 the array value is 6
The debugger identifes this as problematic:
return leader(boss, boss[x]); // finds the boss of boss[x] so on forth.
The array has no element == 0, so the recursion never ends
We're creating a module that will later be used in the next assignment. The reason for this is so that we can check if something is equivalent or maybe even if we need to debug an array. On codeblocks (which is what I normally use) it crashes, but on Linux (what we must code in and then turn it in via submission command to the terminal) it gives a segmentation fault.
Line 13 : Try this line.
int i = 0;

Your first array element is not initialized and it is undefined.

1
2
3
4
5
6
7
8
9
10
11
int leader(ER boss, int x)
{
    if (boss[x] == 0)
    {
        return x;
    }
    else
    {
        return leader(boss, boss[x]); // finds the boss of boss[x] so on forth.
    }
}


This is a huge problem. If x is 1 then the condition is false and you just call the function the same way it was called before.

(boss[1] == 1)

This results in an infinite loop and worse, it crashes your program because the code is recursive.
So we're told to define the function newER to return an array of n+1 integers.

My Professor told us to use that exact function, but since it causes infinite recursion, is there a case that I can use in a scenario that there is NO value of 0's within the array itself? Or is that causing the recursive case to be redundant?
It is better you just post your full assignment so that we can see.
My Professor told us to use that exact function

Is it the function leader? Where is this function given is the assignment document??
Yes, it's function description #2 located under the

"Implementation of an Equivalence Relation Manager"

section.
No, I only saw instructions, not code by your professor.
So did you make the leader function yourself?
It was from notes copied directly from the whiteboard
So I've tried this..

if (boss[x] == x)
{
return x;
}


which allows the exit case to actually be reached, and now it allows me to get correct testing results.
Topic archived. No new replies allowed.