Avoid printing garbage memory from vector container?

I am at the early stages of creating a VERY simple graph. The code below reads commands from a text file such as (for testing purposes):

1
2
3
add a g
add d t
add o p


However, when it pushes back vp1 into the vector near the end of the while loop at line 39, it doesn't seem to keep the value/pointer that exists right before it pushes it, as the small debug messages that I've inserted show. Specifically, the for-loop that goes through the vector prints out the same garbage value. But when I simply push back the test value vp2 from line 20 outside of the while loop, it prints that value out properly but none of the others.

The main code:

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include "graph.h"

int main(int argc, char **argv){

        ifstream fin(argv[1]);

        vector<vertex*> vertices;
        string cmnd;
        char v1, v2;
        bool check = false;

        vertex A = vertex('i');
        vertex *vp2 = &A;
        vertex *vp1;
        vertices.push_back(vp2);


        while (fin >> cmnd >> v1 >> v2){
        cout << "v1 input: " << v1 << endl;

                if (cmnd == "add"){
                        for (int i = 0; i < vertices.size(); i++){
                                cout << "checking for existing vertices..." << endl;
                                if (vertices[i]->label == v1){
                                        check == true;
                                }

                        }

                        if (check == false){
                        cout << "Vertex Does not exist. Creating new vertex: " << v1 << endl;
                        vp1 = createVertex(v1);
                        cout << "New Vertex in while loop: " << vp1->label << endl;
                        vertices.push_back(vp1);
                        }
                }

        }
        cout << "All vertices in container: " << endl;
        for (int i = 0; i < vertices.size(); i++){
                cout << vertices[i]->label << endl;
        }


}


The header:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef GRAPH_H
#define GRAPH_H
#include <vector>
#include <algorithm>
using namespace std;


class vertex {

private:
        vector<vertex> adjacent;
        bool visited;

public:
        vertex(char a);
        char label;

};

vertex* createVertex(char b);


The source:

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

#include <iostream>
#include <vector>
#include <algorithm>
#include "graph.h"
using namespace std;



vertex::vertex(char a){
        this->label = a;
}

vertex* createVertex(char x){
        vertex newVertex = vertex(x);
        vertex *v = &newVertex;
        return v;
}



Of course, I expect it has something to do with the way the createVertex() function returns its value.....
Any help is greatly appreciated!
Last edited on
1
2
3
4
5
vertex* createVertex(char x){
        vertex newVertex = vertex(x);
        vertex *v = &newVertex;
        return v;
}
If a function returns a pointer to a local, the caller cannot safely dereference the pointer because the pointed-to object was destructed when the function returned. Dynamically allocate an object with new and return the pointer, or change vertices to std::vector<vertex> (no pointers).

By the way, I'm not sure what you're trying to do, but at first glance the vertex class looks ill-defined to me.
The pointer returned from createVertex is a pointer to a local variable that stops existing as soon as the function returns, so you cannot dereference the returned pointer without evoking undefined behavior, since it doesn't point to a valid object.

Is there a reason you can't store vertex objects directly?
Last edited on
ugh I reverted back to storing vertex objects directly because I have non idea how I ended up using pointers lol. Definitley much better! Just had a total brain crap-out. Thanks for the help guys! I really appreciate it. And yes, data structure is a mess right now but I'm at the early stages of testing all of this out. Still sonewhat new.
Topic archived. No new replies allowed.