[Qt-interest] QVector with a custom type

Thomas Dähling t.daehling at googlemail.com
Wed Jul 22 10:44:36 CEST 2009


Of course it crashes, you are trying to use a value that does not
exist. Use RegData.push_back() instead of RegData.data()[0], the []
operator for a vector requires the existance of that element.
Alternately, use resize as you did.

2009/7/22 OS Prog <osprog at gmail.com>:
> Hi,
> I try to use QVector with a custom type:
>
> typedef struct
> {
>     quint32 RegAddress;
>     QVector<quint32> RegData;
> }regdata_t;
>
> Q_DECLARE_TYPEINFO(regdata_t, Q_PRIMITIVE_TYPE);
>
>
> QVector<regdata_t> rdata; <-- declared in the steck
>
> when I resize "RegData" to 1 and assign a value:
>
>     rdata.resize(1);
>     rdata.data()[0].RegAddress = DEVID;
>     rdata.data()[0].RegData.resize(1);
>     rdata.data()[0].RegData.data()[0] = 1; <---
>
> the programm crashes:
>
> Unhandled exception at 0x0042e6d1 in testusb.exe: 0xC0000005: Access
> violation reading location 0x00000004.
>
> Regards,
> Nikolay
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
>




More information about the Qt-interest-old mailing list