Why does this crash?

J

JoeC

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?
 
J

Jonathan Lee

  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
 
J

JoeC

  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

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.
 
M

mzdude

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.
 

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,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top