An access violation (segmentation fault) raised in program

This is a discussion on An access violation (segmentation fault) raised in program within the C++ Programming forums, part of the General Programming Boards category; hello guys i wrote a program in dev cpp and when i compile it with 0 warnings or error the ...

  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
    Katy, Texas
    Posts
    2,309
    How about posting the whole error message.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,656
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 02:29 PM
  5. Segmentation fault in beginning or program
    By tameeyore in forum C Programming
    Replies: 1
    Last Post: 02-26-2005, 07:16 PM

Tags for this Thread


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