CImg icon indicating copy to clipboard operation
CImg copied to clipboard

cimg_forXY(img,x,y) version CImg 2.91

Open pwplus7 opened this issue 5 years ago • 2 comments

Can't figure out why this code runs OK :

#include "CImg.h" using namespace cimg_library; typedef unsigned char u8 ;

int main (int argc,char **argv) { u8 m= 0; int del = 20; int W=del *16, H=del *16, x, y, k, kk ; m= 100; CImg img= CImg (W, H,1,1) ;

for (y=0; y< H; y += del) for (x=0; x< W; x +=del) // cimg_forXY(img,x,y) { for (k= 0; k<del; ++k) for (kk= 0; kk<del; ++kk) img(x+kk, y+k)= m ; m++ ; }

BUT this code generates runtime error " malloc(): corrupted top size, Aborted " ... // for (y=0; y< H; y += del) for (x=0; x< W; x +=del) cimg_forXY(img,x,y) ...

pwplus7 avatar Sep 07 '20 17:09 pwplus7

The second loop cimg_forXY(img,x,y) is definitely not the same a the first for (y=0; y< H; y += del) for (x=0; x< W; x +=del), as the latter uses a step of del for both x and y variables. With the second loop, you'll be doing invalid memory accesses (when x==W-1 and y==H-1, and k>1 or kk>1 for instance). It's not so surprising then that you end up scrambling your memory content and getting some weird errors.

dtschump avatar Sep 07 '20 19:09 dtschump

OK, I get it now ... namy thanks

On Mon, Sep 7, 2020 at 3:14 PM David Tschumperlé [email protected] wrote:

The second loop cimg_forXY(img,x,y) is definitely not the same a the first for (y=0; y< H; y += del) for (x=0; x< W; x +=del), as the latter uses a step of del for both x and y variables. With the second loop, you'll be doing invalid memory accesses (when x==W-1 and y==H-1, and k>1 or kk>1 for instance). It's not so surprising then that you end up scrambling your memory content and getting some weird errors.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/dtschump/CImg/issues/288#issuecomment-688479986, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHJ6BCY5QOKI3N7NTRWEABTSEUWHTANCNFSM4Q6UN7BA .

pwplus7 avatar Sep 08 '20 14:09 pwplus7