[try Beta version]
Not logged in

 
 
Array not work (stupid question)

Jun 26, 2020 at 3:31pm
hi community,
i have a problem with array, i don't understand because it go in Segmentation error.


1
2
3
4
5
6
7
8
9
10
11
12
13
class Graph
{
private:
    int nodes;
    int edges;
    pair <long long, pair<int, int> > p[MAX];  
public:
void init()
{
     DisjointSet *Q;
cout << "before init"<<endl;
     Q->init();
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

const int MAX = 9000;  

class DisjointSet
{
private:
    int id[MAX];
public:
 void init()  
 {  
    for(int i = 0;i < MAX;++i)  
        id[i] = i; 
    cout << "after init" << endl; 
 }
}


1
2
3
4
5
6
7
8
9
10
 Graph g;
    string filename = "name.txt";

cout << "sono main" << endl;

    if(!g.file(filename))
    {
        cerr << "errore file" << endl;
    }
    return 0;


 
error: Segmentation error


1
2
3
shell show:
before init
Segmentation error
Last edited on Jun 26, 2020 at 3:43pm
Jun 26, 2020 at 3:37pm
1
2
3
     DisjointSet *Q;
cout << "before init"<<endl;
     Q->init();
Your pointer is uninitialized.
Nowhere do you create a DisjointSet object.

This would create an actual object:
DisjointSet Q;

Of course, that object only exists for the duration of the function... so in your example it's quite useless.

BTW, unless there's a good reason not to, any "initialization" work should be done in the constructor, that's what a constructor is for.
Last edited on Jun 26, 2020 at 3:41pm
Jun 26, 2020 at 3:44pm
if try Q only, this is the error:

1
2
3
in function `Graph::file(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
main4.cpp:(.text._ZN5Graph4fileENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN5Graph4fileENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x25): undefined reference to `DisjointSet::DisjointSet()'
collect2: error: ld returned 1 exit status


for initialization in the costructor, call init in constructor?
Jun 26, 2020 at 3:51pm
Sure, you can call init in the constructor. Or just move the code that's in init into the constructor. Whatever you feel is the best organization.

Can you show the current code you have that produces that error?
Show where you declare and implement the DisjointSet constructor. What you are presenting to us does not match what you have in your code, therefore you are not accurately showing your classes (for example, you are attempting to use a function called "file" but no such function exists).
Last edited on Jun 26, 2020 at 3:53pm
Jun 26, 2020 at 4:12pm
ok i have moved the init code in constructor, now work, thanks.

I have another question related to similar questions,
if I wanted to put a dynamic value in place of MAX,
how should I proceed to then initialize the array?
the dynamic value is in another class
Jun 26, 2020 at 6:15pm
If the size of the array needs to be dynamic, I would switch to using a std::vector.

A vector can be used in the same way as an array, except you would declare it like:
vector<int> id; // empty vector

vector<int> id(42); // vector with 42 ints

id[3]; // index 3 (4th element) of vector

See example at bottom of https://en.cppreference.com/w/cpp/container/vector
Last edited on Jun 26, 2020 at 6:17pm
Jun 26, 2020 at 8:22pm
thanks for all :D
Last edited on Jun 26, 2020 at 9:46pm
Topic archived. No new replies allowed.