GreycLab / CImg

The CImg Library is a small and open-source C++ toolkit for image processing
http://cimg.eu
Other
1.48k stars 282 forks source link

cimg_forXY(img,x,y) version CImg 2.91 #288

Open pwplus7 opened 4 years ago

pwplus7 commented 4 years ago

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

dtschump commented 4 years ago

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.

pwplus7 commented 4 years ago

OK, I get it now ... namy thanks

On Mon, Sep 7, 2020 at 3:14 PM David Tschumperlé notifications@github.com 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 .