Like Tree4Likes
  • 4 Post By Elysia

Segmentation fault 11

This is a discussion on Segmentation fault 11 within the C++ Programming forums, part of the General Programming Boards category; I am trying merge sort an array of 5 numbers. g++ compiles the program without error but i am unable ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Location
    New Delhi, India, India
    Posts
    6

    Segmentation fault 11

    I am trying merge sort an array of 5 numbers. g++ compiles the program without error but i am unable to execute due to segmentation fault 11. I have read the article on segmentation faults. I think writing off array endings might be responsible. How do I correct it?

    Code:
    :
    Code:
    #include <fstream>
    #include <iostream>
    
    
    using namespace std;
    
    
    void sort(int A[], int size)
    {
        if (size==1)
                return;
        
        int left[size/2], right[size/2+1], j=0, k=0;
        
        for(int i=1; i<=size; i++)
        {
            if(i<=size)
                left[++j] = A[i];
            else
                right[++k] = A[i];
        }
        
        sort(left, j);
        sort(right, k);
        
        int a=1, l=1, r=1;
        while(l+r <= j+k)
        {
            if((left[l]<right[r]) || (r==k))
                A[a++]=left[l++];
            else
                A[a++]=right[r++];
        }
    }        
    
    
    int main()
    {
        int array[5]={4,5,1,0,10};
        
        sort(array, 5);
        
        for(int j=1; j<=5; j++)
            cout<<array[j]<<endl; 
        
        return 1;
    }

  2. #2
    Registered User
    Join Date
    Jun 2012
    Posts
    3
    Quote Originally Posted by Chanakya View Post
    I am trying merge sort an array of 5 numbers. g++ compiles the program without error but i am unable to execute due to segmentation fault 11. I have read the article on segmentation faults. I think writing off array endings might be responsible. How do I correct it?

    Code:
    :
    Code:
    #include <fstream>
    #include <iostream>
    
    
    using namespace std;
    
    
    void sort(int A[], int size)
    {
        if (size==1)
                return;
        
        int left[size/2], right[size/2+1], j=0, k=0;
        
        for(int i=1; i<=size; i++)
        {
            if(i<=size)
                left[++j] = A[i];
            else
                right[++k] = A[i];
        }
        
        sort(left, j);
        sort(right, k);
        
        int a=1, l=1, r=1;
        while(l+r <= j+k)
        {
            if((left[l]<right[r]) || (r==k))
                A[a++]=left[l++];
            else
                A[a++]=right[r++];
        }
    }        
    
    
    int main()
    {
        int array[5]={4,5,1,0,10};
        
        sort(array, 5);
        
        for(int j=1; j<=5; j++)
            cout<<array[j]<<endl; 
        
        return 1;
    }
    you're doing

    left[++j] = A[i];

    while your i is from 1 to 5
    your array indexes are 0 to 4

    that should help you figure out the problems

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Some notes:
    C++ does not support variable length arrays, you're clearly using some non-standard compiler extension. I would advise against that.
    You seem to be unaware that arrays start at zero in C++. An 'array[5]' has its items at positions: [0], [1], [2], [3], and [4]. 5 is not valid.
    The function clearly fails when given zero items to sort. How about making that first if statement check for < 2 rather than == 1?
    Rethink line 17. It makes no sense to be retesting the same expression as the for loop condition.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    As has already been mentioned, in C-like languages it's very rare to see a loop like
    Code:
    for(int j=1; j<=5; j++)
    Normally, because array indices begin at 0, you'd write code like this:
    Code:
    for(int j = 0; j < 5; j ++)
    1-based array accesses are fairly common for pseudo-code but they don't really work in code. At least in languages like C++.
    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
    Registered User
    Join Date
    Jun 2012
    Location
    New Delhi, India, India
    Posts
    6
    I have made the above changes but segmentation fault still persists.

    Code:
    #include <fstream>
    #include <iostream>
     
     
    using namespace std;
     
     
    void sort(int A[], int size)
    {
        if (size==1)
                return;
         
        int left[2], right[3], j=0, k=0;
         
        for(int i=0; i<size; i++)
        {
            if(i<=size/2)
                left[j++] = A[i];
            else
                right[k++] = A[i];
        }
         
        sort(left, j);
        sort(right, k);
         
        int a=0, l=0, r=0;
        while(l+r <= j+k)
        {
            if((left[l]<right[r]) || (r==k))
                A[a++]=left[l++];
            else
                A[a++]=right[r++];
        }
    }        
     
     
    int main()
    {
        int array[5]={4,5,1,0,10};
         
        sort(array, 5);
         
        for(int j=0; j<5; j++)
            cout<<array[j]<<endl; 
         
        return 1;
    }

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    2,577
    With size equal 5 what does size/2 equal?

    NOTE: The location left[2] is passed the array bounds.

    Tim S.

    Code:
        int left[2], right[3], j=0, k=0;
          
        for(int i=0; i<size; i++)
        {
            if(i<=size/2)
                left[j++] = A[i];
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,612
    Chanakya, I sincerely recommend you use std::array or std::vector instead of C arrays.
    Then get familiar with the debugger. Don't wait on us to tell you what's wrong. This a good time to get some experience with debugging errors.
    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
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    As much as we do suggest not using that C++ extension I had never seen used before and advised against, you should make sure that you write the function such that it will work for arbitrary sizes. Right now, it could only work when passed a size between 1 and 5. You need some form of dynamic memory allocation.

    Being able to sort zero items is very important too. Ever had an empty folder sorted by name in explorer? Did it crash?
    Or do you expect every single place that could ever call this function to do the check for zero iteself? That could be a lot of places!

    Have more of a think about lines 27 and 29. On line 29, or-ed statements are evaluated left to right, with short-circuit-evaluation.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation Fault
    By DeanWinchester in forum C Programming
    Replies: 6
    Last Post: 01-09-2012, 06:18 AM
  2. Help Getting Segmentation fault
    By godofdoom in forum C Programming
    Replies: 6
    Last Post: 12-21-2010, 01:09 PM
  3. Help with segmentation fault please!
    By JRusty15 in forum C Programming
    Replies: 2
    Last Post: 11-04-2008, 08:38 AM
  4. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 10:09 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