Thread: Segmentation Fault -_-

  1. #16
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Have you tried to print out the values of newX and newY right before the following line:
    Code:
            while (((grid[newX + 1][newY] || 
                     grid[newX - 1][newY] || 
                     grid[newX][newY - 1] || 
                     grid[newX][newY + 1]) != 1)
                  )
    This is the line causing your segmentation fault. The problem I saw was newX was equal to 9 so 9 + 1 would be out of bounds.

    Jim

  2. #17
    Registered User
    Join Date
    Dec 2011
    Posts
    11
    Ooga,

    While that code does eliminate all the segmentation errors, I am still facing the massive problem that it does not check if the "adjacent" element horizontally and vertically, has a value equal to 1.

    The loop is meant to end as soon as newX and newY are adjacent to a particle with say, a and b co-ordinates. The way I've designed it is by assigning particle(a,b) with a value 1 (aka the element grid[a][b] = 1, and all other elements in **grid = 0. The while loop does a random walk, checking random elements in the grid based on this random walk to see if it does end up next to this particle, and then stops.

  3. #18
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Yes, but if the "random walk" walks outside valid indices, it accesses or modifies invalid elements. You need to tackle your (cough!) major problem so, when checking "adjacent" elements, you avoid walking outside valid bounds.

    That is your little challenge to fix - the fix is trivial. If you knowingly insist on using a broken technique, you cannot complain when the operating system keeps detecting that your program is accessing memory it shouldn't and terminating your program. The operating system is doing its job. You need to do yours.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #19
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Try this.
    Code:
            while (!(newX < size-1 && grid[newX+1][newY] ||
                     newX > 0      && grid[newX-1][newY] ||
                     newY > 0      && grid[newX][newY-1] ||
                     newY < size-1 && grid[newX][newY+1])) {

  5. #20
    Registered User
    Join Date
    Dec 2011
    Posts
    11
    Ooga,

    It still doesn't work man. Segmentation faults are killing me arghh.

    Grumpy,

    It may seem "trivial" to you because you have years of experience. However I'm still starting out and have been studying C for one month. It takes me longer to solve problems like this. So stop being an ass.

  6. #21
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Bro View Post
    Grumpy,

    It may seem "trivial" to you because you have years of experience. However I'm still starting out and have been studying C for one month. It takes me longer to solve problems like this. So stop being an ass.
    You might struggle a little with solving it, but you will eventually have an "aha!" moment, and realise how trivial it is. You will learn more from that process than you will by getting someone to fix it for you.

    If I solve the problem, I will learn nothing and you will learn nothing.

    So stop being an ass and get on with it.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #22
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    The newY below should be newX.
    Fix that and use the while condition I gave you.
    Code:
                else if(rnd == 2)
                 {
                     if(newY == 1)
                     {
                             step = step * (-1);
                     }
                     newX = newX - (step);
                 }
    Also, it newX should actually be compared to 0, not 1.

  8. #23
    Registered User
    Join Date
    Dec 2011
    Posts
    11
    Yess! It works now man! Thank you so much for your help Ooga dude. And the rest of you guys for your time.

  9. #24
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I made this pic with your program (size=50, noPars=250)
    Code:
                                  *    *
                                  * ****
                                  * *
                              *   ***
                              *   * *
                        *    ******
                 *      *      *
                 *     **      *
                 *      **   * *
                 **      **  ***
                  *  **   ** ****
        ** *      ******   **** *
        ****           *     *  *         **
           *           ****  *            **
           *     *       *** *           **
           * *****     *  ****   ***      *  *
        ****** * **********  * ****  *  * *****
        *****  *  *   *   * ************* **    *
       ** *          **   * ** * * **  ******** *
        * **          *   *         **        ***
     **** *          **   *         *         *
     **   *        *****  **        *         *
      *   **         * *  *        **
         **          *    *     *****
          *        ****            ***
          *          *             * *
                                  **
                                 ***
                                  *
                                  **
                                   *

  10. #25
    Registered User
    Join Date
    Dec 2011
    Posts
    11
    I just realized that by putting all my source code up here that I might get plagiarized. This was meant to be a final project for one of my modules at college...
    Is there any way to remove the sections where I posted my full code and just leave the section of code where I posted the while loop? Otherwise I'm gonna be screwed if everyone starts handing assignments with my code all over it. Please reply moderators as this is kind of urgent, we have severe consequences for any plagiarism. The only reason why I posted the full code is so that everyone who was helping me could get an idea of my initializations of arrays and variables used in my loop, otherwise I just would have posted the loop by itself...

  11. #26
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Bro View Post
    I just realized that by putting all my source code up here that I might get plagiarized. This was meant to be a final project for one of my modules at college...
    Is there any way to remove the sections where I posted my full code and just leave the section of code where I posted the while loop? Otherwise I'm gonna be screwed if everyone starts handing assignments with my code all over it. Please reply moderators as this is kind of urgent, we have severe consequences for any plagiarism. The only reason why I posted the full code is so that everyone who was helping me could get an idea of my initializations of arrays and variables used in my loop, otherwise I just would have posted the loop by itself...
    Naah. I suggest that it would be quite reasonable that moderators allow your plagiarism (having effectively got others to complete your homework for you) remain here for all to see. You can accept the consequences of your own action.

    You might, incidentally, want to look at this site's homework policy - which you have just openly admitted to having contravened. Look here.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  12. #27
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If this is really your own code, then there isn't much to worry about. You can always say "look, I published first, so I can't possibly have copied it". Providing the terms of your assignment permitted outside debugging help (like posting on a forum), you should be in the clear.

    Deleting things from the board beaks the whole "share and learn" ethos. How many historic posts have you read, and how much have you learnt?
    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.

  13. #28
    Registered User
    Join Date
    Dec 2011
    Posts
    11
    The only thing I needed help with was my while loop, not all of my code. I realize now that I shouldn't have posted my full code in the first place, it was pointless and unnecessary . Is there no way you can just keep the while loop which I included by itself in post no.8 and just delete my FULL source code that i've posted? No one is going to learn anything from it and it'd make my life much easier.

  14. #29
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > The only thing I needed help with was my while loop, not all of my code.
    Not so - there were issues all over the place.

    For example, whilst your broken memory allocation checks were not responsible for the crash, the fact remains the code was wrong and was correctly commented on. If you ignore that, then you're in the wrong class.

    > I realize now that I shouldn't have posted my full code in the first place, it was pointless and unnecessary
    That's for us to decide - not you.

    Had you only posted the loop, we would simply have asked you to post the whole code anyway. Diagnosing segfaults without ALL the code is a crap-shoot. We've seen plenty of posts where the "fault" code looked just fine, only to find out much later that the author had completely screwed up the memory allocation.

    No, I'm not going to delete your posts.
    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.

  15. #30
    Registered User
    Join Date
    Dec 2011
    Posts
    11
    Surely you're being a bit of a fascist douche; I created those posts and I've given you good valid reason as to why I don't think they should be kept up. The "problems all over my code" weren't contributing to the problem I was asking help for.

    And @grumpy, you should really go and lose your virginity dude, it'd help with your evident emotional problems.

    But w/e don't take it down, idc.
    Last edited by Bro; 12-27-2011 at 04:38 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmentation fault!!
    By Necromancer in forum C++ Programming
    Replies: 3
    Last Post: 02-26-2008, 08:03 AM
  2. Segmentation fault
    By (Slith++) in forum C++ Programming
    Replies: 4
    Last Post: 05-06-2007, 03:47 AM
  3. segmentation fault??
    By snappleapple in forum C Programming
    Replies: 9
    Last Post: 04-27-2007, 11:56 PM
  4. Segmentation Fault
    By warfang in forum C++ Programming
    Replies: 9
    Last Post: 04-23-2007, 01:42 AM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM