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

Why does a vector fail here?

0 views
Skip to first unread message

JoeC

unread,
Jan 24, 2010, 4:39:21 PM1/24/10
to
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

unread,
Jan 24, 2010, 5:12:09 PM1/24/10
to
On Jan 24, 11:39 pm, JoeC <enki...@yahoo.com> wrote:
> 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<BYTE> bitData(size, 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;
>   }

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.

Jorgen Grahn

unread,
Feb 6, 2010, 6:57:58 PM2/6/10
to
On Sun, 2010-01-24, JoeC wrote:
> 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

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

0 new messages