Solved

memcpy vector to array in QT

Posted on 2011-03-07
14
2,280 Views
Last Modified: 2012-05-11
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
Comment
Question by:PeterVarain
  • 4
  • 3
  • 3
  • +1
14 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
ID: 35061677
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
 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 125 total points
ID: 35061698
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
 
LVL 86

Assisted Solution

by:jkr
jkr earned 250 total points
ID: 35061699
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
 
LVL 35

Expert Comment

by:mccarl
ID: 35061720
Oh yeah, plus what jkr says about the amount of data to memcpy.
0
 
LVL 35

Expert Comment

by:mccarl
ID: 35061737
jkr, you still need to take the address of the first parameters
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Author Comment

by:PeterVarain
ID: 35067114
I've tried all the suggestions but I still get the same error...

Regards,
Peter
0
 
LVL 32

Assisted Solution

by:sarabande
sarabande earned 125 total points
ID: 35068086
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
 
LVL 86

Expert Comment

by:jkr
ID: 35071612
>>"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
 
LVL 32

Expert Comment

by:sarabande
ID: 35094543
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
 
LVL 35

Expert Comment

by:mccarl
ID: 35124597
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
 
LVL 32

Expert Comment

by:sarabande
ID: 35126726
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now