Code fails under vc8 - clearing a structure

J

Jack

Hi, This code I inherited worked under VC7 now fails under VC8 with error:
error C2664: 'memcpy' : cannot convert parameter 1 from
'std::_Vector_iterator<_Ty,_Alloc>' to 'void *'



vector <png_color> vPalette;
..
..
..
memcpy(vPalette.begin(), palette, num_palette * sizeof png_color);

png_color is part of the PNG graphics library so I can't change that.
So, what is the best way to substitute the memcpy()?

Thanks
 
H

Heinz Ozwirk

Jack said:
Hi, This code I inherited worked under VC7 now fails under VC8 with error:
error C2664: 'memcpy' : cannot convert parameter 1 from
'std::_Vector_iterator<_Ty,_Alloc>' to 'void *'



vector <png_color> vPalette;
.
.
.
memcpy(vPalette.begin(), palette, num_palette * sizeof png_color);

png_color is part of the PNG graphics library so I can't change that.
So, what is the best way to substitute the memcpy()?

First of all you should be greatful that the new compiler has found such a nasty bug in that code.

Basically you should replace that memcpy with a loop that assigns a new value to each element of the vector. Depending on what palette, num_palette etc. are std::copy or some other function from algorithm might be usefull.

HTH
Heinz
 
G

Gavin Deane

Jack said:
Hi, This code I inherited worked under VC7 now fails under VC8 with error:
error C2664: 'memcpy' : cannot convert parameter 1 from
'std::_Vector_iterator<_Ty,_Alloc>' to 'void *'



vector <png_color> vPalette;
.
.
.
memcpy(vPalette.begin(), palette, num_palette * sizeof png_color);

png_color is part of the PNG graphics library so I can't change that.
So, what is the best way to substitute the memcpy()?

It looks like you've fallen foul of the fact that vector iterators
*can* be implemented as pure pointers, but they need not be. To avoid
falling into this trap again, never assume implementation details like
that. begin() returns an iterator so use it where an iterator is
expected. Pretend you don't know that internally that iterator *might*
be a pointer.

You haven't shown full code, so I'm going to assume the following:
palette is an array of png_color objects
the size of the array palette is num_palette
the size of the vector vPalette is >= num_palette

std::copy(palette, palette + num_palette, vPalette.begin());

should do what you want.

Gavin Deane
 
K

klaus hoffmann

Jack said:
Hi, This code I inherited worked under VC7 now fails under VC8 with error:
error C2664: 'memcpy' : cannot convert parameter 1 from
'std::_Vector_iterator<_Ty,_Alloc>' to 'void *'



vector <png_color> vPalette;
.
.
.
memcpy(vPalette.begin(), palette, num_palette * sizeof png_color);

png_color is part of the PNG graphics library so I can't change that.
So, what is the best way to substitute the memcpy()?

Thanks

as Heinz pointed out you might use std::assign

hth
Klaus
 
M

Michiel.Salters

Jack said:
Hi, This code I inherited worked under VC7 now fails under VC8 with error:
error C2664: 'memcpy' : cannot convert parameter 1 from
'std::_Vector_iterator<_Ty,_Alloc>' to 'void *'

vector <png_color> vPalette;
.
memcpy(vPalette.begin(), palette, num_palette * sizeof png_color);

png_color is part of the PNG graphics library so I can't change that.
So, what is the best way to substitute the memcpy()?

std::copy(pallete, pallete + num_palette, vPalette.begin());

HTH,
Michiel Salters.
 
J

Jack

Gavin Deane said:
It looks like you've fallen foul of the fact that vector iterators
*can* be implemented as pure pointers, but they need not be. To avoid
falling into this trap again, never assume implementation details like
that. begin() returns an iterator so use it where an iterator is
expected. Pretend you don't know that internally that iterator *might*
be a pointer.

You haven't shown full code, so I'm going to assume the following:
palette is an array of png_color objects
the size of the array palette is num_palette
the size of the vector vPalette is >= num_palette

std::copy(palette, palette + num_palette, vPalette.begin());

should do what you want.

Thanks, seems to work - compiles anyway :) .

Is there a similar std substitution for memset or must I step through as
Heinz suggested? ( I don' really know the std library)
memset(pV, 0, nPal * sizeof png_color);

fails with the same error.
 
J

Jack

Jack said:
Thanks, seems to work - compiles anyway :) .

Is there a similar std substitution for memset or must I step through as
Heinz suggested? ( I don' really know the std library)
memset(pV, 0, nPal * sizeof png_color);

Should read:
memset(vPalette.begin(), 0, nPal * sizeof png_color);
 
J

Jack

Jack said:
Thanks, seems to work - compiles anyway :) .

Is there a similar std substitution for memset or must I step through as
Heinz suggested? ( I don' really know the std library)
memset(pV, 0, nPal * sizeof png_color);

Should read:
memset(vPalette.begin(), 0, nPal * sizeof png_color);
 
G

Gavin Deane

Get yourself a copy of this

http://www.josuttis.com/libbook/

It widely recommended and worth it's weight in gold.
Should read:
memset(vPalette.begin(), 0, nPal * sizeof png_color);

Not quite, because memset directly sets the values of individual bytes
of memory without caring whether those bytes values make sense for the
type of data. But you should avoid that sort of low level operation
unless you absolutely need it. Try

std::fill_n(vPalette.begin(), nPal, png_color());

to fill the first nPal elements of the vector with default-constructed
png_color objects.

Gavin Deane
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top