3

I have trouble with pointers and references to doubles.

I want to access elements in QVector by names. The vector contains doubles:

QVector<double> properties;
properties.append(28.0);
properties.append(1.0);
properties.append(44.0);
properties.append(0.001);

Now I create pointers to doubles:

double* Amplitude;
double* Frequency;
double* PhaseDifference;
double* Stepsize;

These pointers should provide access to the elements of my vector:

Amplitude = &properties[0];
Frequency = &properties[1];
PhaseDifference = &properties[2];
Stepsize = &properties[3];

In my opinion dereferencing these pointers should give me the correct values, but it doesn't. In this case I got zeros for the first two pointers and the third and fourth were correct.

I tried to use more entries in the vector and the result was that only the last two had the correct values. What is going wrong there?

I create and print the values in the constructor. Printing of the vector gives the right values!

Does anybody have an idea?

Tony the Pony
  • 40,327
  • 71
  • 187
  • 281
user1147533
  • 33
  • 1
  • 5
  • 1
    Did you modify the vector? That could invalidate all pointers. – Šimon Tóth Jan 13 '12 at 11:16
  • Aside: if you've got a small constant number (four) of named properties, you shouldn't be using a `QVector` at all. – Fred Foo Jan 13 '12 at 11:17
  • 1
    Yes...I did :( Do you mean resizing could move the vector?? ... That makes sense, because it needs neighbored memory cells... thanks for that idea...I will change the order of the code and check the result :D – user1147533 Jan 13 '12 at 11:19
  • Oh yes...that was the problem!! I had the wrong order in my code. The resizing of the vector was invalidating the references. Thank you very much ;) – user1147533 Jan 13 '12 at 11:28
  • http://stackoverflow.com/questions/6438086/iterator-invalidation-rules – Lightness Races in Orbit Jan 13 '12 at 11:31

4 Answers4

4

Your named pointers are in fact iterators. Iterators can be invalidated. For example, whenever you resize the vector, or insert anything into them, etc. Look up the exact rules of iterator invalidation for your particular vector type, in this case, QVector and see if you've performed any of those iterator invalidating operations prior to printing. Incidentally, dereferencing an invalidated iterator may result in undefined behavior.

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
  • 1
    more correct to say would be that vector iterators are just pointers. More precisely, you cannot construct an iterator from a pointer, but the reverse is possible. – rubenvb Jan 13 '12 at 11:26
  • 1
    @rubenvb: No that is not more correct. Pointers ***are*** vector iterators, i.e. they satisfy the rquiremnts of ***iterator concept***. But other things can serve as iterators as well. For example, in MSVC debug mode, a vector iterator is a class which checks boundaries – Armen Tsirunyan Jan 13 '12 at 11:28
3

You must be doing something wrong. This works:

#include <QVector>
#include <QDebug>

int main()
{
    QVector<double> v;
    v.append(2.0);
    v.append(18.4);

    double* val1 = &v[0];
    double* val2 = &v[1];

    qDebug() << *val1 << "\n" << *val2 << "\n";
}

Reasons for things going wrong are:

  • vector is reallocated (resized+moved to another position in memory)
  • elements are removed from the vector.
  • output is done incorrectly, or you're printing memory addresses instead of the values.
rubenvb
  • 74,642
  • 33
  • 187
  • 332
2

You should set size of vector when you initialize them. Everything will be fine until you change them (push/pop) after that values in ur pointers will be undefined.

gingray
  • 391
  • 8
  • 17
1

There is a possibility that your pointers may have been invalidated between the time you've obtained them, and the time you've actually used them.

This can happen if the QArray is resized (by adding more elements than it can currently accommodate).

Tony the Pony
  • 40,327
  • 71
  • 187
  • 281