Thread: An access violation (segmentation fault) raised in program

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    1

    An access violation (segmentation fault) raised in program

    hello guys

    i wrote a program in dev cpp and when i compile it with 0 warnings or error
    the program doesnt run for me
    in addition when i debug this program it wrote to me:
    "An access violation (segmentation fault) raised in program"

    what is the problem?
    insert
    Code:
    "
    int path(int* b,int n);
    
    int findmin(int* board,int n);
    
    #include <stdio.h>
    
    #include <stdlib.h>
    
    int main()
         {
           int b[15]={3,4,5,5,6,9,9,8,5,4,7,5,3,1,2};
    
           int n=5,p;
    
           p=path(b+14,n);
    
           printf("%d",p);
    
           system("pause");
    
           return 0;
        }
    
     int path(int* b,int n)
     {
     int m=0;
    
     if(n==1){return *b;}
    
     m=findmin(b,n);
    
     return (m+path(b-n,n--));
     }
     int findmin(int* board,int n)
        {
              int i=0,min=0;
    
              for(i=n;i>0;i--)
                {
                    if(board[i]>min)
                      {
                           min=board[i];
                        }
                  }
         return min;
         }
    "
    thanks

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    How about posting the whole error message.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    A segmentation fault happens when you try to access memory out of your bounds. You have a pointer to an array of 15 integers. If you ever incremented or decremented it so that it no longer pointed inside that array - you would most likely get a segmentation fault.

    Trace through the execution of your code - and see if you can find a situation where you've accidentally allowed your pointer to go out of bounds.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > return (m+path(b-n,n--));
    You've used the post-decrement, so all you've managed to do is

    return (m+path(b-n,n));

    The -- happens, but the result vanishes.

    Try
    return (m+path(b-n,n-1));
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmentation fault... first time with unix...
    By theMethod in forum C Programming
    Replies: 16
    Last Post: 09-30-2008, 02:01 AM
  2. Strange segmentation fault
    By Ron in forum C Programming
    Replies: 24
    Last Post: 06-15-2008, 02:10 PM
  3. Access Violation?
    By rwmarsh in forum C++ Programming
    Replies: 6
    Last Post: 05-04-2006, 10:56 AM
  4. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  5. Segmentation fault in beginning or program
    By tameeyore in forum C Programming
    Replies: 1
    Last Post: 02-26-2005, 08:16 PM

Tags for this Thread