Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

memcpy vector to array in QT

Posted on 2011-03-07
14
Medium Priority
?
2,732 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +1
14 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 1000 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 36

Assisted Solution

by:mccarl
mccarl earned 500 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 1000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 36

Expert Comment

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

Expert Comment

by:mccarl
ID: 35061737
jkr, you still need to take the address of the first parameters
0
 

Author Comment

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

Regards,
Peter
0
 
LVL 35

Assisted Solution

by:sarabande
sarabande earned 500 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 35

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 36

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 35

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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

650 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