M
Markus Dehmann
I discovered a strange pointer behavior that screws my program. Below
is a mini code example.
Basically, I have a vector with data and want a function (getLongest)
to find one of these values. The function should return a pointer to
the value. In the function, everything works, it finds the correct
element, but the returned pointer points to an empty element (but not
NULL). It's very strange. Can someone explain what's wrong with the
example?
--------------------------------------
#include <string>
#include <vector>
#include <iostream>
using namespace std;
class Data{
public:
Data(string val){
data = val;
length = data.length();
}
inline int getLength(){return length;}
inline int getData(){return data;}
private:
string data;
int length;
};
Data *getLongest(vector<Data> data){
Data *longest;
for(int i=0; i < data.size(); i++){
if(data.getLength() > longest->getLength()){
longest = &data;
}
}
cerr << " Longest is " << longest->data << endl; // correct output
return longest;
}
int main(){
vector<Data> myData;
myData.push_back(Data("one"));
myData.push_back(Data("three"));
myData.push_back(Data("four"));
Data *longest = getLongest(myData);
cerr << "main(): Longest is " << longest->data << endl; // incorrect
}
--------------------------------------
I wanted that getLongest() returns a pointer, not the value itself, so
that I can be sure that, when I change that value later, the original
element in the vector is changed. Is that a correct assumption?
Another problem: To iterate over the vecor, I would like to use an
iterator, but that does not work:
for ( vector<Data>::iterator it = data.begin(); it != data.end();
++it ){
if(it->getLength() > longest->getLength()){
longest = it; // ??? or:
longest = *it; // what do I have to say here???
}
}
Thanks for every hint! (grrr, switching from Java to C++ is really
hard!!)
Markus
is a mini code example.
Basically, I have a vector with data and want a function (getLongest)
to find one of these values. The function should return a pointer to
the value. In the function, everything works, it finds the correct
element, but the returned pointer points to an empty element (but not
NULL). It's very strange. Can someone explain what's wrong with the
example?
--------------------------------------
#include <string>
#include <vector>
#include <iostream>
using namespace std;
class Data{
public:
Data(string val){
data = val;
length = data.length();
}
inline int getLength(){return length;}
inline int getData(){return data;}
private:
string data;
int length;
};
Data *getLongest(vector<Data> data){
Data *longest;
for(int i=0; i < data.size(); i++){
if(data.getLength() > longest->getLength()){
longest = &data;
}
}
cerr << " Longest is " << longest->data << endl; // correct output
return longest;
}
int main(){
vector<Data> myData;
myData.push_back(Data("one"));
myData.push_back(Data("three"));
myData.push_back(Data("four"));
Data *longest = getLongest(myData);
cerr << "main(): Longest is " << longest->data << endl; // incorrect
}
--------------------------------------
I wanted that getLongest() returns a pointer, not the value itself, so
that I can be sure that, when I change that value later, the original
element in the vector is changed. Is that a correct assumption?
Another problem: To iterate over the vecor, I would like to use an
iterator, but that does not work:
for ( vector<Data>::iterator it = data.begin(); it != data.end();
++it ){
if(it->getLength() > longest->getLength()){
longest = it; // ??? or:
longest = *it; // what do I have to say here???
}
}
Thanks for every hint! (grrr, switching from Java to C++ is really
hard!!)
Markus