0
void initialize(int arr[], int size[], int n)
{
    int i;
    for(i = 1; i <= n; i++) {
        arr[i] = i;
        size[i] = 1;
    }

}

class hell
{
    public:
    int edges;
    int vertices;
    pair<int , pair<int,int>> p[100000];
    int disjoint_set[10000];
    int cc_size[10000]; // size of connected components

    hell(int e, int v)
    {
      edges = e;
      vertices = v;

      initialize(disjoint_set, cc_size, vertices);
    }
};

In the following class when I create an object using vertices=100000 and edges=100000, the code stops working. But when we remove the initialize(disjoint_set, cc_size, vertices) it starts working. I don't have any clue to such behavior. Please guide me.

8
  • 2
    What do you mean it "stops working"? Commented Apr 24, 2018 at 13:57
  • 4
    100000 is far bigger tan 10000. Commented Apr 24, 2018 at 13:57
  • 4
    Array indices start at 0 and not 1, you have UB, change your loop bounds. Furthermore, if create a local hell variable, you are going to have huge arrays on the stack, this may not be a good idea (especially the 100k array of pair of pair... ). Commented Apr 24, 2018 at 14:00
  • 1
    Does it just hang? Throw an error? What about if you give it smaller values for verticies and edges? Commented Apr 24, 2018 at 14:00
  • 2
    Why not simply use a vector and allocate the size you actually needs instead of having a huge underlying array that you partially use? Commented Apr 24, 2018 at 14:04

1 Answer 1

3

Arrays in C++ are zero indexed, which means that valid index is in [0..n[ range. Your code does it wrong:

 for(i = 1; i <= n; i++) {
    arr[i] = i;
    size[i] = 1;
}

it should be:

 for(i = 0; i < n; i++) {
    arr[i] = i + 1;
    size[i] = 1 + 1;
}

or better use algo std::iota() and std::fill():

std::iota( arr, arr + n, 1 );
std::fill( size, size + n, 1 );

and you better use std::vector, which will adjust its size properly, rather than have huge array.

Sign up to request clarification or add additional context in comments.

3 Comments

You don't want std::iota for size, you want std::fill.
@Holt thanks, did not see that there is 1 not i, they are close.
Thanks a lot !!! I have one more question, when I initialized the vertices = 10000 and edges = 100000, the code works but assigns hell.edge = 1 which seems weird. Is their any reason behind it ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.