Segfault Woes

This is a discussion on Segfault Woes within the C++ Programming forums, part of the General Programming Boards category; I keep getting a segfault with the following code, the only thing I have been able to gather is that ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    15

    Segfault Woes

    I keep getting a segfault with the following code, the only thing I have been able to gather is that I always get the segfault when "val" is 2.

    *stopped,reason="signal-received",signal-name="SIGSEGV",signal-meaning="Segmentation fault",thread-id="1",frame={addr="0x004015f4",func="theoFlood",a rgs=[{name="x",value="1"},{name="y",value="4"},{name="v al",value="2"}],file="../src/solve.cpp",fullname="C:/Users/Admin/workspace/Flood It/Debug/../src/solve.cpp",line="86"}
    Code:
    int efficient[6];
    extern const int gridsize = 14;
    extern int grid[gridsize][gridsize];
    
    
    [. . .]
    
        for (int z = 0; z < 6; z++) // Theoretically runs through all possibilities
          {
            for (int x = 0; x < gridsize; x++)
              for (int y = 0; y < gridsize; y++)
                if (grid[x][y] == z)
                  theoFlood(x, y, z + 1);
          }
    
    [. . .]
    void theoFlood(int x, int y, int val)
      {
        efficient[val - 1]++;
        
        if (x)
          if (grid[x-1][y] == val)
            theoFlood(x-1, y, val);
        if (y)
          if (grid[x][y-1] == val)
            theoFlood(x, y-1, val);
        if (grid[x+1][y] == val)
          theoFlood(x+1, y, val);
        if (grid[x][y+1] == val)
          theoFlood(x, y+1, val);
      }

  2. #2
    Registered User
    Join Date
    Mar 2008
    Posts
    12

    only 2 members

    Hey Blurr can you help me with a function?

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > if (grid[x+1][y] == val)
    When x is 13, x+1 will equal 14, which is outside the bounds of your array.

    > if (grid[x][y+1] == val)
    Same here for y+1.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,001
    http://cpwiki.sf.net/Buffer_overrun
    Do you normal arrays? If so, consider using std::vector and using the .at member function which will throw if out-of-bounds access is made.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    15
    I'll read up on vectors, thanks. But for now, I've changed the code to

    Code:
    void theoFlood(int x, int y, int val)
      {
        efficient[val - 1]++;
    
        if (x)
          if (grid[x-1][y] == val)
            theoFlood(x-1, y, val);
        if (y)
          if (grid[x][y-1] == val)
            theoFlood(x, y-1, val);
        if (x < gridsize)
          if (grid[x+1][y] == val)
            theoFlood(x+1, y, val);
        if (y < gridsize)
          if (grid[x][y+1] == val)
            theoFlood(x, y+1, val);
      }
    The only variable I'm trying to write here is efficient, which doesn't seem like it will ever go out of bounds. Anyone know why it will crash only when "val" = 2, and not 1?
    Last edited by Blurr; 04-05-2008 at 09:06 AM.

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    15
    Quote Originally Posted by MuzicMedia View Post
    Hey Blurr can you help me with a function?
    I'd start a thread about it.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,001
    Most likely another out of bounds. Grid is another array, just as efficient. So turn them into vector and use .at instead of []. Then see if you catch any out of bounds.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Oct 2004
    Posts
    25
    maybe more of the grid is '2' than 1.

    It looks to me like there's nothing stopping the recursive theoflood from continuously searching into negative values of x (or y). If it keeps hitting 'val' in the theoFlood, it will get closer and closer to negatives, eventually reaching theoFlood(-1....) and trying to access memory that it shouldn't be (at grid[-1][...]).

  9. #9
    Registered User
    Join Date
    Jul 2007
    Posts
    15
    Quote Originally Posted by Swordsalot View Post
    maybe more of the grid is '2' than 1.

    It looks to me like there's nothing stopping the recursive theoflood from continuously searching into negative values of x (or y). If it keeps hitting 'val' in the theoFlood, it will get closer and closer to negatives, eventually reaching theoFlood(-1....) and trying to access memory that it shouldn't be (at grid[-1][...]).
    Isn't that stopped with

    > if (x)

    and

    > if (y)
    ?

    Elysia: Will do

  10. #10
    Registered User
    Join Date
    Jul 2007
    Posts
    15
    When I try to catch out_of_range g++ says that that it has not been declared. Any clues as to how I'd fix it?

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,001
    It's called std::out_of_rage.
    In header file stdexcept. Did you include that too?
    Last edited by Elysia; 04-05-2008 at 02:07 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >    if (x < gridsize)
    >      if (grid[x+1][y] == val)
    >        theoFlood(x+1, y, val);
    You step out-of-bounds here. Assume x=13, then x+1=14. The indices of grid[] run from 0 to 13. grid[14] is one past the end.

  13. #13
    Registered User
    Join Date
    Jul 2007
    Posts
    15
    It was that combined with infinite recursion. I've fixed it. Thanks for the help guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segfault with Linked List Program
    By kbrandt in forum C Programming
    Replies: 1
    Last Post: 06-23-2009, 08:13 AM
  2. Segfault with additional variable?
    By misterFry in forum C++ Programming
    Replies: 11
    Last Post: 11-12-2008, 10:55 AM
  3. malloc() resulting in a SegFault?!
    By cipher82 in forum C++ Programming
    Replies: 21
    Last Post: 09-18-2008, 12:24 PM
  4. use of printf prevents segfault!
    By MK27 in forum C Programming
    Replies: 31
    Last Post: 08-27-2008, 01:38 PM
  5. Segfault and Warning help
    By Uncle Rico in forum C Programming
    Replies: 1
    Last Post: 03-25-2005, 02:51 PM

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