Why does a vector fail here?

J

JoeC

I am writing a program to display bitmaps and I want to use a vector
to hold my bitmap data but when I use this algorithm it fails:

for(index = 0; index < dwn; index++){
memcpy(&temp[((dwn-1) - index)*acc],
&bitData[index*acc],acc);}

The vector is declared in the header and in the constuctor I fill it
with zeros as a default.

Size is = acc*dwn;
Default 16*16 bitmap;

for(int lp = 0; lp != size; lp++){
bitData.push_back(0);
}

If I add this the program does not crash:

BYTE* bitmap::flip(){

int acc = bmi.bmiHeader.biWidth;
int dwn = bmi.bmiHeader.biHeight;
int index;

BYTE *temp2 = new BYTE[bitData.size()];
for(int l=0; l < bitData.size();l++){
temp2[l]=bitData[l];
}

BYTE * temp = new BYTE[acc*dwn];

for(index = 0; index < dwn; index++){
memcpy(&temp[((dwn-1) - index)*acc],
&temp2[index*acc],acc);}

return temp;
}

I have written a bitmap before and used a vector to hold the graphics
data but this object trying to do a DIB bitmap is giving me all kinds
of problems. The algorithm works in a function but when I try to put
the same information in an object it fails. Anything else I can add
to help explain the problem. I often have problems asking questions
on more complex problems because it can be cumbersome to post from
several files both .h and .cpp.
 
Ö

Öö Tiib

I am writing a program to display bitmaps and I want to use a vector
to hold my bitmap data but when I use this algorithm it fails:

  for(index = 0; index < dwn; index++){
    memcpy(&temp[((dwn-1) - index)*acc],
           &bitData[index*acc],acc);}


Seems algorithm like any other how it fails? This sounds like simple
typo outside of what you have posted.
The vector is declared in the header and in the constuctor I fill it
with zeros as a default.

The vector named 'temp'? Usually people do not declare such vectors in
header files.

Size is = acc*dwn;
Default 16*16 bitmap;

This can not compile. You have to describe a context where it
compiles.
  for(int lp = 0; lp != size; lp++){
    bitData.push_back(0);
     }

Usually this is done in constructor like:
std::vector said:
If I add this the program does not crash:

BYTE* bitmap::flip(){

  int acc = bmi.bmiHeader.biWidth;
  int dwn = bmi.bmiHeader.biHeight;
  int index;

  BYTE *temp2 = new BYTE[bitData.size()];
  for(int l=0; l < bitData.size();l++){
    temp2[l]=bitData[l];
  }

  BYTE * temp = new BYTE[acc*dwn];

  for(index = 0; index < dwn; index++){
    memcpy(&temp[((dwn-1) - index)*acc],
           &temp2[index*acc],acc);}

  return temp;
  }

Yes. If this does not crash then all is fine with bitData vector.
Problem is with these temps of yours ... or odd variables with 3
character names.
I have written a bitmap before and used a vector to hold the graphics
data but this object trying to do a DIB bitmap is giving me all kinds
of problems.  The algorithm works in a function but when I try to put
the same information in an object it fails.  Anything else I can add
to help explain the problem.  I often have problems asking questions
on more complex problems because it can be cumbersome to post from
several files both .h and .cpp.

Do not worry. Free land and non-moderated group. Make example (as
lengthy as you made non-crashing one) with vector usage that crashes.
It is highly probable you discover your mistake in the process.
 
J

Jorgen Grahn

I am writing a program to display bitmaps and I want to use a vector
to hold my bitmap data but when I use this algorithm it fails:

for(index = 0; index < dwn; index++){
memcpy(&temp[((dwn-1) - index)*acc],
&bitData[index*acc],acc);}

This is completely out of context so I have no idea what you're doing,
but memcpy() is dangerous (and almost always pointless) to use on
anything which may be an object -- for example a std::vector.

(It's also used pointlessly surprisingly often in C code -- I have
lost count of all the complicated memcpy() calls I've replaced
with straight struct assignments over the years.)

....
Anything else I can add
to help explain the problem. I often have problems asking questions
on more complex problems because it can be cumbersome to post from
several files both .h and .cpp.

http://en.wikipedia.org/wiki/Pastebin

perhaps with the core part of the problem quoted in the posting.

/Jorgen
 

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,769
Messages
2,569,582
Members
45,060
Latest member
BuyKetozenseACV

Latest Threads

Top