[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2793
  • Last Modified:

memcpy vector to array in QT

Hi everybody,

I have a small problem here... I need to copy the contents of a vector to a certain position in an array. However, memcpy returns the error "invalid conversion from int to void*"
Here's an example of what I'm trying to do:

QVector<qint16> Vector1(1024);
QVector<qint16> Vector2(1024);
QVector<qint16> Vector3(1024);

int HArr[1024][1024];

memcpy(HArr[0][0], &Vector1[0], 1024);
memcpy(HArr[0][1], &Vector2[0], 1024);
memcpy(HArr[0][2], &Vector3[0], 1024);

Can someone show me how to do this correctly ?

Thanks,
Peter

0
PeterVarain
Asked:
PeterVarain
  • 4
  • 3
  • 3
  • +1
4 Solutions
 
jkrCommented:
Just make that
memcpy(HArr[0][0], Vector1.constData(), 1024);
memcpy(HArr[0][1], Vector2.constData(), 1024);
memcpy(HArr[0][2], Vector3.constData(), 1024);

Open in new window


See also http://doc.qt.nokia.com/latest/qvector.html and http://doc.qt.nokia.com/latest/qvector.html#constData in particular.
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
I'm not sure what the internal state of a QVector is so you might have to play with this some more, but the error that you got was to do with the first argument. Instead of ...

memcpy(HArr[0][0], &Vector1[0], 1024);
memcpy(HArr[0][1], &Vector2[0], 1024);
memcpy(HArr[0][2], &Vector3[0], 1024);

try...


memcpy(HArr[0], &Vector1[0], 1024);
memcpy(HArr[1], &Vector2[0], 1024);
memcpy(HArr[2], &Vector3[0], 1024);
0
 
jkrCommented:
Ooops, correction, you need to take the size of the data type into account - also make sure that you are using a target array data tape that matches qint16, i.e.
QVector<qint16> Vector1(1024);
QVector<qint16> Vector2(1024);
QVector<qint16> Vector3(1024);

short int HArr[1024][1024]; // 'short int' as the equivalent to 'quint16'

memcpy(HArr[0][0], Vector1.constData(), 1024 * sizeof(quint16));
memcpy(HArr[0][1], Vector2.constData(), 1024 * sizeof(quint16));
memcpy(HArr[0][2], Vector3.constData(), 1024 * sizeof(quint16));

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Oh yeah, plus what jkr says about the amount of data to memcpy.
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
jkr, you still need to take the address of the first parameters
0
 
PeterVarainAuthor Commented:
I've tried all the suggestions but I still get the same error...

Regards,
Peter
0
 
sarabandeCommented:
the memcpy takes a pointer as first argument and not an int. also the second index runs faster than second. so it should be

if (sizeof(quint16) == sizeof(short int))
{
    memcpy(&HArr[0][0], Vector1.constData(), 1024 * sizeof(quint16));
    memcpy(&HArr[1][0], Vector2.constData(), 1024 * sizeof(quint16));
    memcpy(&HArr[2][0], Vector3.constData(), 1024 * sizeof(quint16));
}

Sara

0
 
jkrCommented:
>>"invalid conversion from int to void*"

Aaah, I think everyone missed that little detail - you are not using the target arrays correctly, that should be
QVector<qint16> Vector1(1024);
QVector<qint16> Vector2(1024);
QVector<qint16> Vector3(1024);

short int HArr[1024][1024]; // 'short int' as the equivalent to 'quint16'

memcpy(HArr[0], Vector1.constData(), 1024 * sizeof(quint16));
memcpy(HArr[1], Vector2.constData(), 1024 * sizeof(quint16));
memcpy(HArr[2], Vector3.constData(), 1024 * sizeof(quint16));

Open in new window

0
 
sarabandeCommented:
thanks.

the last code of jkr also works cause  &HArr[0][0]  and HArr[0] both point to first element of the first row of HArr.

did you notice the

 if (sizeof(quint16) == sizeof(short int))

i added?

the point is that it is dangerous when using memcpy to calculate the size to copy from the elements of the right side when the left side elements have a different type.

a short int not necessarily must have 16 bits but could grow in future. so if a system would use a 64-bit int, it very likely would make short int 32-bit.

Sara
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
I think both jkr and I deserve some of the points (in addition to sarabande) as jkr was the first person to get the correct pointer to copy from (http:#a35061677) and the correct size to copy (http:#a35061699) and I was the first person to get the correct destination pointer to copy to (http:#a35061698). Sara had the idea about checking for the size of the data items (http:#a35068086)
0
 
sarabandeCommented:
mccarl is right. i didn't see the 'Instead of'  in his solution or i would have refered to.

an equal split would be best.

Sara
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 4
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now