Solved

memcpy vector to array in QT

Posted on 2011-03-07
14
2,420 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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
 

Author Comment

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

Regards,
Peter
0
 
LVL 33

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 33

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 33

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
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…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

839 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