1 Attachment(s)
[Qt] Pointer auto-deassignment problem.
Hello there.
I've been struggling with this problem for 2 days [~30hours]. I'm writing my master thesis, and I'm stuck with pointers. Below I'll present some sample code. But what's the problem?
I have lots of QVector<myClass*> and when I'm operating on these myClass pointers values sometimes disappear. In sample below it, when I merge two WordClasses, parentClass of one of the words disappears. Don't know why.
Ok. Here are the classes:
----------
class dictionary
{
QVector<word> slowa;
public:
dictionary();
word* addWord();
int size();
word* getWord(int i);
};
---------
word* dictionary::addWord()
{
word nowe;
this->slowa.push_back(nowe);
return &slowa[slowa.size()-1];
}
int dictionary::size()
{
return slowa.size();
}
word* dictionary::getWord(int i)
{
if (i<slowa.size() && i>=0)
return &slowa[i];
return 0;
}
----------
class word
{
QString name;
wordclass *parentClass;
QVector<word*> nextWords;
public:
word();
void setParentClass(wordclass *wc);
wordclass* getParentClass();
void addWord(word *w);
QVector<word*> getWords();
void setName(QString n);
QString getName();
};
word::word()
{
}
void word::setParentClass(wordclass *wc)
{
wordclass *w;
w = wc;
this->parentClass = w;
}
wordclass* word::getParentClass()
{
return parentClass;
}
void word::addWord(word *w)
{
nextWords.push_back(w);
}
QVector<word*> word::getWords()
{
return nextWords;
}
void word::setName(QString n)
{
name = n;
}
QString word::getName()
{
return name;
}
----------
class wordclass
{
QVector<word*> words;
int id;
public:
wordclass(int i=0);
void addWord(word*);
void delWord(word*);
int size();
word* getWord(int i);
QVector<word*> getWords();
int getId();
};
wordclass::wordclass(int i):id(i)
{
words.push_back(0);
}
void wordclass::addWord(word *w)
{
this->words.push_back(w);
w->setParentClass(this);
}
void wordclass::delWord(word *w)
{
this->words.remove(words.indexOf(w));
}
int wordclass::size()
{
return words.size();
}
word* wordclass::getWord(int i)
{
return words[i];
}
QVector<word*> wordclass::getWords()
{
return words;
}
int wordclass::getId()
{
return this->id;
}
----------
class wordclasses
{
QVector<wordclass> wektor;
int internalPointer;
public:
wordclasses();
wordclass* addWordClass();
wordclass* getWordClass(int i);
int size();
void merge(int i,int j);
};
wordclasses::wordclasses()
{
internalPointer = 0;
}
wordclass* wordclasses::addWordClass()
{
wordclass nowa(++internalPointer);
this->wektor.push_back(nowa);
return &wektor.last();
}
int wordclasses::size()
{
return wektor.size();
}
wordclass* wordclasses::getWordClass(int i)
{
if (i<wektor.size() && i>=0)
return &wektor[i];
return 0;
}
void wordclasses::merge(int i, int j)
{
wordclass *i_,*j_;
i_ = getWordClass(i);
j_ = getWordClass(j);
QVector<word*> words;
words = i_->getWords();
for (int z=0;z<words.size();z++)
{
words[z]->setParentClass(j_);
i_->delWord(words[z]);
j_->addWord(words[z]);
}
this->wektor.remove(i);
}
----------
----------
And in mainclass:
word *ptr;
ptr = dict.addWord();
ptr->setName("one");
ptr = dict.addWord();
ptr->setName("two");
ptr = dict.addWord();
ptr->setName("three");
doSomethingWithWord();
initWordClasses();
for (int i=0;i<wordClasses.size();i++)
{
wordclass *wc;
wc = wordClasses.getWordClass(i);
qDebug()<<"WC: "<<QString::number(wc->getId());
QVector<word*> wds;
wds = wc->getWords();
for (int j=0;j<wds.size();j++)
{
//CRASHES HERE. There's no ParentClass.
qDebug()<<"->"<<wds[j]->getName()<<wds[j]->getParentClass()->getId();
}
}
I've no idea what's wrong. I guess it's something with pointer assignment, but couldn't figure out what :/
If in method I do something like this:
{
word *ptr;
ptr = variable.getWordPointer();
myVectorOfWordPointers.push_back(ptr);
}
Then it looks like ptr after execution of method disappears, like a normal variable. <br/>And is it allright to do something like this:
ptr* class::method()
{
return &myInnerVectorOfVars[0];
}
Will it return pointer to the element at position 0 in Vector? Or will it return pointer to variable returned from vector [volatile one].
I'm in deep wood, and don't know what to do :/
Code in tar.gz in attachment.
Re: [Qt] Pointer auto-deassignment problem.
Quote:
Originally Posted by
xyliosist
Hello there.
I've been struggling with this problem for 2 days [~30hours]. I'm writing my master thesis, and I'm stuck with pointers. Below I'll present some sample code. But what's the problem?
First, please re-edit your code to use code tags. The code you posted is practically impossible to read if it is not formatted using code tags.
Quote:
I have lots of QVector<myClass*> and when I'm operating on these myClass pointers values sometimes disappear. In sample below it, when I merge two WordClasses, parentClass of one of the words disappears. Don't know why.
Have you used the debugger that comes with your compiler suite? The debugger is the tool that you are supposed to use to debug programs and figure these things out.
Quote:
Code:
return &myInnerVectorOfVars[0];
}
Will it return pointer to the element at position 0 in Vector?
I have no idea what a QVector is, but if it were std::vector, then that line of code returns the address of the first element in the vector, given that the vector is not empty.
Regards,
Paul McKenzie
Re: [Qt] Pointer auto-deassignment problem.
Very hard to follow with code tags and indentation ...
1) I don't see the following functions (maybe a missed them)
[code]
doSomethingWithWord();
initWordClasses();
[code]
2) In many places you return the address of an element in the QVector ...
Do you store those values and use them later ? If so, there would be a problem
if you add to the slowa vector in the above example. The address might not
be valid do to internal re-allocation in QVector
Re: [Qt] Pointer auto-deassignment problem.
All of you, I'm sorry for not using code syntax, in hurry couldn't find it.
Philip, and that's what I was worrying about [pt 2].
How can I avoid this?
I have 2 classes which have inside QVector<someClass>.
And lots of classes that use pointers to that someClass. Maybe instead of using QVector I should use ordinary array?
Re: [Qt] Pointer auto-deassignment problem.
Quote:
Originally Posted by
xyliosist
Maybe instead of using QVector I should use ordinary array?
Assuming that QVector acts the same as vector, then how would an array help you? There won't be any difference.
The bottom line is that you are mismanaging pointers, and you need to fix this issue. It doesn't matter if you were using arrays, vectors, or QVector.
Regards,
Paul McKenzie
Re: [Qt] Pointer auto-deassignment problem.
[sorry for writing another post, but couldn't find "edit this post" button]
and what If I have QVector<someClass*> vector and I want to return one of the pointers?
Is
return vector[0];
returning someClass* pointer? or something else? [I know, stupid question, but I got confused]
Re: [Qt] Pointer auto-deassignment problem.
so Paul, what can I do?
Creating "long enough" vector, not needing to expand, isn't a choice.
How does Map work? It re-allocates too?
Re: [Qt] Pointer auto-deassignment problem.
Quote:
Originally Posted by
xyliosist
All of you, I'm sorry for not using code syntax, in hurry couldn't find it.
Then your code will remain unread by many that could help you:
Code tags are this:
[code] Your code goes here [/code]
Second, please post this "word" class, as it is important and plays a big role in your code now, since you are storing pointers to it.
Why do you need to make a Qvector of word pointers? What's wrong with just QVector<word>? Then you would eliminate a lot of these pointer manipulations you have going on now, if "word" need not be stored as pointers.
Regards,
Paul McKenzie
Re: [Qt] Pointer auto-deassignment problem.
1) I'f you'd be so kind to tell me where can I edit my post.
2) why do I need QVector of word pointers? Because I need to have only one instance of word appearing in many classes like
- word needs to know what was next/previous word;
- wordClass needs to know what words are in it;
- sentence needs to know what words it have;
and when I change for example word's string, this change must be seen in other classes.
Re: [Qt] Pointer auto-deassignment problem.
Quote:
Originally Posted by
xyliosist
1) I'f you'd be so kind to tell me where can I edit my post.
Quote:
Originally Posted by
xyliosist
1) I'f you'd be so kind to tell me where can I edit my post.
Go to your first post. Do you see an "Edit" button at the bottom, along with "Quote"?
Regards,
Paul McKenzie
Re: [Qt] Pointer auto-deassignment problem.
when reallocating does whole vector move? or only it's elements?
Following this http://doc.qt.nokia.com/qq/qq19-containers.html, if I understand properly, the location of vector's head is persistent, only the elements move? So I could for example store vector's position and in place of someClass* store integer working as an offset? So calling someClass* would be equivalent to vector_s_position+int?
Re: [Qt] Pointer auto-deassignment problem.
-reply with qoute
-multi-quote
-quick reply
that's what I see Paul.
And thanks for replies :)
Re: [Qt] Pointer auto-deassignment problem.
I guess I should use QList instead of QVector. If I understood right, lists are not reallocated.
Re: [Qt] Pointer auto-deassignment problem.
Quote:
Originally Posted by
xyliosist
when reallocating does whole vector move? or only it's elements?
Following this
http://doc.qt.nokia.com/qq/qq19-containers.html, if I understand properly, the location of vector's head is persistent, only the elements move? So I could for example store vector's position and in place of someClass* store integer working as an offset? So calling someClass* would be equivalent to vector_s_position+int?
Assuming QVector works similar to std::vector, you cannot safely store a pointer to any element in the vector when you add elements to it. That holds for the first element just as well. However, std::vector has an overloaded operator [] that allows you to retrieve any element using an index. So you could store indices instead of pointers and access the element using operator [], as long as you don't remove elements from the vector, of course.