HELP! this easy code locks my computer!

This is a discussion on HELP! this easy code locks my computer! within the C Programming forums, part of the General Programming Boards category; hi, i have a boundaryFill() function and I want it to paint pixel by pixel the area limited x and ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    3

    HELP! this easy code locks my computer!

    hi,
    i have a boundaryFill() function and I want it to paint pixel by pixel
    the area limited x and y

    x is limited 10 to 100
    y is limited 10 to 100

    and I call function boundaryFill(50,50);
    what is wrong with this code it locks my computer.
    Code:
    void boundaryFill(int x, int y)
        {
            if ((x < 10) || (x >= 100)) return;
            if ((y < 10) || (y >= 100)) return;
    
            Form1->Canvas->Pixels[x][y]=clRed;
    
                boundaryFill(x+1,y);
                boundaryFill(x,y+1);
                boundaryFill(x-1,y);
                boundaryFill(x,y-1);
            
        }
    thanks very much for your answers

  2. #2
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    A note: You're painting each pixel several times. Work it out for yourself with a small case.
    Away.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Code:
    void boundaryFill(int x, int y) {
            if(Form1->Canvas->Pixels[x][y] == clRed) return;  /* it's already red! */
            if ((x < 10) || (x >= 100)) return;
            if ((y < 10) || (y >= 100)) return;
    
            Form1->Canvas->Pixels[x][y]=clRed;
    
            boundaryFill(x+1,y);
            boundaryFill(x,y+1);
            boundaryFill(x-1,y);
            boundaryFill(x,y-1);        
    }
    Beware, I did a similar function on my computer, and if you let if fill in larger than 200x200, you run out of stack space.

    [edit]
    The above code does a flood fill.
    [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    If you don't want a flood fill, start in the upper left corner and work your way downwards:

    Code:
    void boundaryFill(int x, int y) {
            if ((x < 10) || (x >= 100)) return;
            if ((y < 10) || (y >= 100)) return;
    
            Form1->Canvas->Pixels[x][y]=clRed;
    
            boundaryFill(x+1,y);
            boundaryFill(x,y+1);
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,334
    Well I did say in your previous post that this algorithm sucked.
    And now you know why - it's an effective stack smasher on most real machines.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    I made a non-recursive version that works just fine, though.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    I'd like to point out to the original poster that the original code is an infinite loop (that, while looping, overflows the stack). It will tunnel to a corner and then alternate back and forth between two pixels.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    You're right, although that had already been pointed out by confuted.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 03:28 AM
  2. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 12:33 PM
  3. how do you code in binary...
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 01-27-2003, 04:14 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. i need help with a code (really easy)
    By yorge in forum C++ Programming
    Replies: 5
    Last Post: 04-21-2002, 01:55 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21