Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why does this crash?

1 view
Skip to first unread message

JoeC

unread,
Dec 25, 2009, 3:12:38 PM12/25/09
to
I am loading a BYTE array into a vector in an object. Then I want to
reverse the order of the array in the vector.

static
BYTE Bits[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,
...

cbm2 = new colorBitmap(Bits,16*16,colors, 12);

colorBitmap::colorBitmap(BYTE * b, int n1, BYTE* rgb, int n2){

int lp;

create();

for(lp = 0; lp != n1; lp++)
bitData.push_back(*b++);

void colorBitmap::flipBitmap(){

std::vector<BYTE>temp;
for(int lp = 0; lp != bitData.size();lp++) <-Fails
temp.push_back(bitData[lp]);

bitData.clear();

for(int lp = bitData.size(); lp !=0; lp--)
bitData.push_back(temp[lp]);

copy(bitData.end(), bitData.begin(), back_inserter(temp)); Fails
as well.

What can I do to work with a vector in a way the program will not
crash?

Jonathan Lee

unread,
Dec 25, 2009, 3:47:23 PM12/25/09
to
On Dec 25, 3:12 pm, JoeC <enki...@yahoo.com> wrote:
>   for(lp = 0; lp != n1; lp++)
>     bitData.push_back(*b++);

Pretty sure you could use bitData.assign(b, b + n1) instead.

>      for(int lp = 0; lp != bitData.size();lp++) <-Fails
>        temp.push_back(bitData[lp]);
>
>      bitData.clear();

temp.swap(bitData);

>      for(int lp = bitData.size(); lp !=0; lp--)
>        bitData.push_back(temp[lp]);

Off by one error. temp[lp - 1]. Also, why wouldn't you begin
lp at temp.size()? They should be the same, I know, but
semantically it makes more sense.

By the end of all this you may as well just do

std::reverse(bitData.begin(), bitData.end());

>     copy(bitData.end(), bitData.begin(), back_inserter(temp)); Fails
> as well.

This starts at the end() of bitData and moves *away* from
begin(), indefinitely. Appending to temp for some reason.
Maybe this is alternative code to the preceding?

Maybe you want std::copy_backward()? Or reverse_copy?

>
> What can I do to work with a vector in a way the program will not
> crash?

I'm not sure why the first fail happens. The latter is because
you're reading past the end of "bitData".

--Jonathan

JoeC

unread,
Dec 25, 2009, 4:27:16 PM12/25/09
to

I am experimenting with my program. It will crash if I try to copy
just one element [0]. I am using the dev c++ free compiler and I
wonder if it has some kind of bug. It gives me a crazy error when I
try to write the destructor for the colorBitmap object. It says:
multiple definition of `colorBitmap::~colorBitmap()'

I send an array to an object then copy that array to a vector then I
want to reverse that vector because they made bitmaps backwards. I
was wondering if I did some kind of pointer error by copying addresses
instead of the values.

mzdude

unread,
Dec 26, 2009, 7:58:06 AM12/26/09
to
On Dec 25, 3:12 pm, JoeC <enki...@yahoo.com> wrote:
> I am loading a BYTE array into a vector in an object.  Then I want to
> reverse the order of the array in the vector.
>
>   static
>   BYTE Bits[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
>                  0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,
> ...
>
>   cbm2 = new colorBitmap(Bits,16*16,colors, 12);
>
> colorBitmap::colorBitmap(BYTE * b,  int n1, BYTE* rgb, int n2){
>
>   int lp;
>
>   create();
>
>   for(lp = 0; lp != n1; lp++)
>     bitData.push_back(*b++);

where is the closing brace for the ctor? What is create()?
limit the scope of lp to the for loop.

>
> void colorBitmap::flipBitmap(){
>
>      std::vector<BYTE>temp;
>      for(int lp = 0; lp != bitData.size();lp++) <-Fails
>        temp.push_back(bitData[lp]);
>

what is bitData?

>      bitData.clear();
>
>      for(int lp = bitData.size(); lp !=0; lp--)
>        bitData.push_back(temp[lp]);
>
>     copy(bitData.end(), bitData.begin(), back_inserter(temp)); Fails
> as well.

should be a reverse_copy().


>
> What can I do to work with a vector in a way the program will not
> crash?

In the future posting the smallest complete example will
help us to help you.

Also spend some time familiarizing youself with the
algorithms in the standard library.

std::reverse() will greatly simplify your task.

0 new messages