vector<> pointer

B

Bob

Hi All,
I defined the following class:

class Graph {
public:
Graph() { }
int genRandGeomGraph(int i_nodes, double i_radius);
void print();
~Graph() {
if( NodeList ) delete [] NodeList;
}
private:
class Node {
public:
Node() {}
Node(int inID) { this->ID = inID; }
inline int getID() {return this->ID;}
inline void setID(int newID) { this->ID = newID; }
inline void setx(double inx) { this->x = inx; }
inline double getx() { return this->x; }
inline void sety(double iny) { this->y = iny; }
inline double gety() { return this->y; }
~Node() { if ( neighbors ) delete [] neighbors; }
private:
double x, y;
int ID;
std::vector<int>* neighbors;
};
std::vector<Node>* NodeList;
};

whenever I try to access NodeList from genRandGeomGraph(...)
int Graph::genRandGeomGraph((int i_nodes, double i_radius) {
NodeList = new vector< Node >(nnodes);
std::cout << NodeList->size();
return 1;
}

I get segmentation fault. Shouldn't I be able to access it?

Thanks
 
K

Kai-Uwe Bux

Bob said:
Hi All,
I defined the following class:

class Graph {
public:
Graph() { }
int genRandGeomGraph(int i_nodes, double i_radius);
void print();
~Graph() {
if( NodeList ) delete [] NodeList;

The test is redundant: deleting a null pointer is a harmless no-op.
}
private:
class Node {
public:
Node() {}
Node(int inID) { this->ID = inID; }
inline int getID() {return this->ID;}
inline void setID(int newID) { this->ID = newID; }
inline void setx(double inx) { this->x = inx; }
inline double getx() { return this->x; }
inline void sety(double iny) { this->y = iny; }
inline double gety() { return this->y; }
~Node() { if ( neighbors ) delete [] neighbors; }

Same here.
private:
double x, y;
int ID;
std::vector<int>* neighbors;
};
std::vector<Node>* NodeList;
};

a) Is there any reason to use vector<>* instead of just vector<>?
b) Your code only shows deallocation, not allocation. So, you are working
with unallocated memory.
c) The pointer member in your class renders the compiler generated copy-
constructor invalid: it will not do the RightThing(tm).
whenever I try to access NodeList from genRandGeomGraph(...)
int Graph::genRandGeomGraph((int i_nodes, double i_radius) {
NodeList = new vector< Node >(nnodes);

So, NodeList is allocated with new, not with new[]. Thus, you would have to
deallocate it using delete, not using delete[].

Again: I don't see any reason to use the pointer.
std::cout << NodeList->size();
return 1;
}

I get segmentation fault. Shouldn't I be able to access it?

Please post a minimal complete example that demonstrates the problem. As
posted, the code looks incomplete: e.g., nnodes does not seem to be
declared. Possibly somewhere in the code you have not shown, there is
undefined behavior.


Best

Kai-Uwe Bux
 
R

red floyd

Hi All,
I defined the following class:

class Graph {
public:
    Graph() { }
    int genRandGeomGraph(int i_nodes, double i_radius);
    void print();
    ~Graph() {
        if( NodeList ) delete [] NodeList;
    }
private:
    class Node {
    public:
        Node() {}
        Node(int inID) { this->ID = inID; }
        inline int    getID() {return this->ID;}
        inline void   setID(int newID) { this->ID = newID; }
        inline void   setx(double inx) { this->x = inx; }
        inline double getx() { return this->x; }
        inline void   sety(double iny) { this->y = iny; }
        inline double gety() { return this->y; }
        ~Node() { if ( neighbors ) delete [] neighbors; }
    private:
        double x, y;
        int ID;
        std::vector<int>* neighbors;
Java-ism. Should be std::vector said:
    };
    std::vector<Node>* NodeList;
Java-ism. Should be std::vector said:
};

whenever I try to access NodeList from genRandGeomGraph(...)
int Graph::genRandGeomGraph((int i_nodes, double i_radius) {
    NodeList = new vector< Node >(nnodes);
No such variable as nnodes.
Should be:
NodeList.resize(nnodes);
    std::cout << NodeList->size();
    return 1;


}

I get segmentation fault. Shouldn't I be able to access it?

Also, as a nitpick, the hungarian notation should be completely
dropped.
 
B

Bob

Hi All,
I defined the following class:
class Graph {
public:
    Graph() { }
    int genRandGeomGraph(int i_nodes, double i_radius);
    void print();
    ~Graph() {
        if( NodeList ) delete [] NodeList;
    }
private:
    class Node {
    public:
        Node() {}
        Node(int inID) { this->ID = inID; }
        inline int    getID() {return this->ID;}
        inline void   setID(int newID) { this->ID = newID; }
        inline void   setx(double inx) { this->x = inx; }
        inline double getx() { return this->x; }
        inline void   sety(double iny) { this->y = iny; }
        inline double gety() { return this->y; }
        ~Node() { if ( neighbors ) delete [] neighbors; }
    private:
        double x, y;
        int ID;
        std::vector<int>* neighbors;

Java-ism.  Should be std::vector said:
    std::vector<Node>* NodeList;

Java-ism.  Should be std::vector<Node> NodeList;


whenever I try to access NodeList from genRandGeomGraph(...)
int Graph::genRandGeomGraph((int i_nodes, double i_radius) {
    NodeList = new vector< Node >(nnodes);

No such variable as nnodes.
Should be:
      NodeList.resize(nnodes);
    std::cout << NodeList->size();
    return 1;

I get segmentation fault. Shouldn't I be able to access it?

Also, as a nitpick, the hungarian notation should be completely
dropped.

Yes, It was new ...() the problem, thank you very much, sometimes I
don't see these things...

Bob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Infinite loop problem 1
Help building first class 11
STL Vector question? 12
Using Constructors -- LNK2019 error in Visual C++ 1
Crossword 2
Array of pointer-to-functions 23
Crossword 14
std::vector RTE(Run Time Error), 2

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top