Thread: C program terminates without message

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    3

    C program terminates without message

    Hello everybody,

    I'm new to C programming. I'm trying to parallelize bottom's up merge sort algorithm. Firstly, i must have working procedural code.

    I rember I had my code working last week but I made some changes ( probably to arguments passed to sort function ), i run my program several times without rebuilding it so i though it's still working.

    Now i came back to my code and my program terminates without any message which confuses me. I debbuged merge function and it seems to work well, at the end i have sorted array.

    Please take a look at my code and try to solve my problem. fprintf doesn't print any value to my stdout.


    whole code : [C] mergesort - Pastebin.com

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Maybe you could paste the code on here in code tags. That format is easier to use rather than going to another site to view and discuss the code.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Next time, please post your code here, in [code][/code] tags. Make sure you post as plain text (our forums will provide syntax highlighting), and that your code is properly formatted. It should look like
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    /*
     * bottoms up merge sort procedural algorithm : http://www.mathcs.emory.edu/~cheung/Courses/171/Syllabus/7-Sort/merge-sort5.html
     * */
    
    
    void printArray(int *arr, int len) {
        for (int i = 0; i < len; i++) {
            fprintf(stdout, " %d", arr[i]);
        }
    }
    
    
    int getBigger(int a, int b) {
        if (a > b) {
            return a;
        } else {
            return b;
        }
    }
    
    
    void merge(int *a, int iLeft, int iMiddle, int iRight, int tmp[]) {
    
    
        int i, j, k;
    
    
        i = iLeft;
        j = iMiddle;
        k = iLeft;
    
    
        while (i < iMiddle || j < iRight) {
            if (i < iMiddle && j < iRight) { // Both array have elements
                if (a[i] < a[j])
                    tmp[k++] = a[i++];
                else
                    tmp[k++] = a[j++];
            } else if (i == iMiddle)
                tmp[k++] = a[j++]; // a is empty
            else if (j == iRight)
                tmp[k++] = a[i++]; // b is empty
        }
    
    
    }
    
    
    void sort(int *a, int *tmp, int length) {
        int width;
    
    
        for (width = 1; width < length; width = 2 * width) {
            // Combine sections of array a of width "width"
            int i;
    
    
            for (i = 0; i < length; i = i + 2 * width) {
                int left, middle, right;
    
    
                left = i;
                middle = i + width;
                right = i + 2 * width;
    
    
                merge(a, left, middle, right, tmp);
    
    
            }
    
    
            /* ========================================================
               Instead of doing multiply copy operations inside merge()
               we perform ONE copy operation after ALL mereg operations
               ======================================================== */
            for (i = 0; i < length; i++)
                a[i] = tmp[i];
        }
    }
    
    
    int main(void) {
    
    
        int array[10] = { 4, 3, 6, 1, 14, 2, 12, 4, 10, 3 };
        int temp[10];
        int length = 10;
    
    
        printArray(&array[0], 10);
    
    
        sort(&array[0], &temp[0], length);
    
    
        printArray(&array[0], 10);
    
    
        return 0;
    }
    Now, are you sure you get no output? Here's what I get when I run your code:
    Code:
    $ make sort
    gcc -Wall -ggdb3 -pedantic -std=c99 -O0 -o sort sort.c -lm -lpthread -lrt
    
    $ ./sort 
    Segmentation fault (core dumped)
    I get a seg fault. That means the program doesn't finish properly, so all output buffers aren't flushed. This is important because stdout is line buffered, meaning it generally doesn't actually send the output to the terminal (it buffers it) until it sees a newline. If you print a newline at the end of printArray, (e.g. putchar('\n')) you should see this:
    Code:
    $ ./sort 
     4 3 6 1 14 2 12 4 10 3
     1 1 2 2 3 4 4 6 12 14
    Segmentation fault (core dumped)
    Use your debugger to figure out why you have repeated elements and why you got a seg fault (most likely array out of bounds).

    EDIT: If you wish to add debugging output, use stderr, which is unbuffered, meaning the output shows up immediately, regardless of presence of a newline. But that should be separate from normal program output. Don't output regular messages to stderr, they belong on stdout.

  4. #4
    Registered User
    Join Date
    Jan 2014
    Posts
    3
    When I run it from command line it shows me segmentation fault.

    Nsight eclipse didn't so thats why I were confused.

    Thanks.

  5. #5
    Registered User
    Join Date
    Jan 2014
    Posts
    3
    It's bad algorithm implementation.

    Valgrind helped me to figure out.
    test driven development ftw.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program prematurely terminates?
    By ScottieK in forum C Programming
    Replies: 1
    Last Post: 03-26-2012, 01:20 AM
  2. Is memory released after program terminates?
    By dbergman in forum C++ Programming
    Replies: 3
    Last Post: 06-09-2010, 11:42 AM
  3. program terminates abruptly
    By roaan in forum C Programming
    Replies: 3
    Last Post: 08-28-2009, 03:53 PM
  4. Storing objects after program terminates
    By ejohns85 in forum C++ Programming
    Replies: 7
    Last Post: 08-25-2009, 06:27 AM
  5. this program just starts and terminates
    By s2theG in forum C Programming
    Replies: 2
    Last Post: 07-17-2005, 03:30 PM