?
Solved

memcpy vector to array in QT

Posted on 2011-03-07
14
Medium Priority
?
2,627 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
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!

 
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

WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

Question has a verified solution.

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

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses
Course of the Month11 days, 18 hours left to enroll

752 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