queue contents lost between functions/classes (c++ noob, experienced with PHP)

I am new to C++. I have a PHP background so 'some' coding experience.

Hopefully this is just a simple RTFN issue - but I have been trying RTFN for some time and am getting nowhere so hopefully someone can easily spot what I am doing wrong.

The code below is not what I am actually doing, but a stripped down example to illustrate where I am having problems.


Here is what I would like to achieve:
I have a class (myData) that is a simple data structure.
I have a class (myClass) that handles collection, storage and subsequent release of data, which stores instances of the above myData class in a queue.
main() creates an instance of myClass, calls a function within myClass that adds some data to the queue.
main() then calls the get function of myClass and passes a variable which myClass should populate with the data from the queue.

in main(), after the last step above I try to read the two variables in the returned myData object. The first is an integer, the second a string. The integer output is random and I get a segfault on the string.

As you will see from the code, I have tested reading these variables at various points to try and pin down where its going wrong. I feel the code and output below will illustrate this much more quickly than me explaining.

My thoughts are that the string variable's destructor is being called for some reason.

Here is my example 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <stdio.h>
//#include "../shared/socketUDP.cpp"
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/fcntl.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h> // for close() for socket
#include <stdlib.h>
#include <iostream>
#include <queue>

using namespace std;

class myData {
public:
    string myString;
    int myInteger;
};

class myClass {
public:
    queue<myData *> _data;

    void populate() {
        myData *_item;
        myData *_item2;
        _item=new myData();
        _item->myString="Test String";
        _item->myInteger=47;

        cout << "myClass->populate() - Before Queue\n";
        cout << "Integer: " << _item->myInteger << "\n";
        cout << "String: " << _item->myString << "\n\n";

        _data.push(_item);
        _item2=_data.front();

        cout << "myClass->populate() - After Queue\n";
        cout << "Integer: " << _item2->myInteger << "\n";
        cout << "String: " << _item2->myString << "\n\n";
    };

    bool get(myData * _temp) {
        _temp=_data.front();

        cout << "myClass->get() - Before Pop\n";
        cout << "Integer: " << _temp->myInteger << "\n";
        cout << "String: " << _temp->myString << "\n\n";

        _data.pop();

        cout << "myClass->get() - After Pop\n";
        cout << "Integer: " << _temp->myInteger << "\n";
        cout << "String: " << _temp->myString << "\n\n";

        return(true);
    };
};


int main() {

    myClass *_test;
    myData *_testItem;
    _test=new myClass();

    _test->populate();
    _test->get(_testItem);

    cout << "main() - After myClass->get()\n";
    cout << "Integer: " << _testItem->myInteger << "\n";
    cout << "String: " << _testItem->myString << "\n\n";
};

(excuse the excessive includes, they are used in my main program and I have just not cleaned them up for this)

Here is the output I get:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
myClass->populate() - Before Queue
Integer: 47
String: Test String

myClass->populate() - After Queue
Integer: 47
String: Test String

myClass->get() - Before Pop
Integer: 47
String: Test String

myClass->get() - After Pop
Integer: 47
String: Test String

main() - After myClass->get()
Integer: 126096
Segmentation fault


I thought this was an issue with passing by reference, and it possibly is but I have tried as many variations of using * and & as I can and this is the only one that actually compiles!
Any suggestions to help me fix this would be very appreciated.
This is the diffrenece between pass by copy and pass by reference.
In main() - You are passing a copy of the _testItem pointer to the _test->get function -so the _testItem variable in main() never gets updated and remains uninitalised which causes the errors when you try to use it.


Fo this code to work - You would to change the _test->get function to take either a pointer to pointer or a reference to a pointer. Using a reference to pointer would be easiest (no need to dereference).

P.S
What I'm saying is just change this one line like this:
bool get(myData * & _temp) { // use a reference to pointer
Last edited on
guestgulkan,

Thank you so much for taking the time to not only provide me with a fix but explain to me clearly what I was doing wrong.

Not only did your suggestion work perfectly but your explanation made it clear to me 'why' it works and what I was doing wrong which will hopefully help me avoid getting into the same situation in the future.

Once again, thank you very much.
Topic archived. No new replies allowed.