Like Tree1Likes
  • 1 Post By Salem

stack failure after the end of the program

This is a discussion on stack failure after the end of the program within the C Programming forums, part of the General Programming Boards category; Hello, I wrote a program that receives 2 different sized arrays sorts them and copies common numbers to a third ...

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

    stack failure after the end of the program

    Hello,

    I wrote a program that receives 2 different sized arrays sorts them and
    copies common numbers to a third array.
    the program works well but in the end it gives and error:

    "Debug error!

    run-time check failure #2 - Stack around the variable "X" was corrupted"

    this error occurs with all three arrays.

    here is the code:
    Code:
    #include<stdio.h>
    void mergesort(int*,int,int);
    void merge(int*,int,int,int);
    void checker_exe3(int*,int*,int,int);
    void arr_mehaber(int*,int*,int*);
    void mymerge(int*,int*,int*,int);
    #define A 5
    #define B 7
    
    
    void main()
    { 
    int a[A]={1,1,1,1,1},low=0,high,i,p=0,k=0;
        int b[B]={1,1,1,1,1,1,1};
        int c[A]={0};
        
        mergesort(a,low,A);
        mergesort(b,low,B);
        mymerge(a,b,c,k);
        
        for(i=1;i<k;i++)
        {
            printf("%d\n",c[i]);
        }
        
        printf("\n");
        return 0;
    }
    void mergesort(int arr[], int low, int high)
    {
        int mid;
        
        if(low<high)
        {
        
            mid=(low+high)/2;
            mergesort(arr,low,mid);
            mergesort(arr,mid+1,high);
            merge(arr,low,high,mid);
        
        }
    }
    
    
        void merge(int arr[], int low, int high, int mid)
        {
            int i, j, k, c[50];
            i=low;
            j=mid+1;
            k=low;
                while((i<=mid) && (j<=high))
                {
                    if(arr[i] < arr[j])
                    {
                        c[k]=arr[i];
                        k++;
                        i++;
                    }
                    else
                    {
                        c[k]=arr[j];
                        k++;
                        j++;
                    }
                }
                while(i <= mid)
                {
                    c[k]=arr[i];
                    k++;
                    i++;
                }
                while(j <= high)
                {
                    c[k]=arr[j];
                    k++;
                    j++;
                }
                for(i=low; i<k; i++)
                    arr[i]=c[i];
    }
    void checker_exe3(int arr[],int res[],int high,int p)
    {
        int i;
        printf("%d\n",high);
        for(i=1; i<high; i++)
        {
            if(arr[i-1] == arr[i])
            {
                res[i-1]=arr[i];
                p++;
                i++;
            }
        }
    }
    void arr_mehaber(int a[],int b[],int arr[])
    {
        int i;
        
        for(i=0; i<A; i++)
            arr[i]=a[i];
        
        for(i=0; i<B; i++)
            arr[A+i]=b[i];
        
        for(i=0;i<(A+B);i++)
            printf("%d,",arr[i]);
    
    
        printf("\n");
    }
    void mymerge(int a[],int b[],int c[],int k)
    {
        int i=0,j=0;
    
    
                while((i<=A) || (j<=B))
                {
                    if(a[i] == b[j])
                    {
                        c[k]=a[i];
                        i++;
                        j++;
                        k++;
                    }
                    if(a[i]>b[j])
                        j++;
                    if(a[i]<b[j])
                        i++;
                    
                }
    for(i=1;i<k;i++)
        printf("%d ", c[i] );
    
    
    printf("\n");
    }
    I would be very grateful if someone would explain me my mistake
    i'm kind of new to C so dont be surprised if it is not very effective

    thanks in advance,

    P.S

    HAPPY NEW YEAR!!

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,899
    Other than an error for the following line:
    Code:
    void main()
    Your code seems to run without errors.

    The function main should be defined to return an int, int main(void). Also when you define a function as void you can not return anything from this function.

    Jim

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,762
    Run normally, I get garbage data - indicating running off the ends of an array.
    Code:
    $ ./a.out 
    1 1 1 1 32569
    Run with valgrind, there are a lot of illegal memory references.
    Code:
    $ valgrind ./a.out
    ==2699== Memcheck, a memory error detector
    ==2699== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
    ==2699== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info
    ==2699== Command: ./a.out
    ==2699== 
    ==2699== Conditional jump or move depends on uninitialised value(s)
    ==2699==    at 0x40076F: merge (bar.c:53)
    ==2699==    by 0x4006FD: mergesort (bar.c:39)
    ==2699==    by 0x4006E6: mergesort (bar.c:38)
    ==2699==    by 0x400612: main (bar.c:17)
    ==2699== 
    ==2699== Conditional jump or move depends on uninitialised value(s)
    ==2699==    at 0x40076F: merge (bar.c:53)
    ==2699==    by 0x4006FD: mergesort (bar.c:39)
    ==2699==    by 0x400612: main (bar.c:17)
    ==2699== 
    ==2699== Conditional jump or move depends on uninitialised value(s)
    ==2699==    at 0x40076F: merge (bar.c:53)
    ==2699==    by 0x4006FD: mergesort (bar.c:39)
    ==2699==    by 0x4006E6: mergesort (bar.c:38)
    ==2699==    by 0x4006E6: mergesort (bar.c:38)
    ==2699==    by 0x400628: main (bar.c:18)
    ==2699== 
    ==2699== Conditional jump or move depends on uninitialised value(s)
    ==2699==    at 0x40076F: merge (bar.c:53)
    ==2699==    by 0x4006FD: mergesort (bar.c:39)
    ==2699==    by 0x4006E6: mergesort (bar.c:38)
    ==2699==    by 0x400628: main (bar.c:18)
    ==2699== 
    ==2699== Conditional jump or move depends on uninitialised value(s)
    ==2699==    at 0x40076F: merge (bar.c:53)
    ==2699==    by 0x4006FD: mergesort (bar.c:39)
    ==2699==    by 0x400628: main (bar.c:18)
    ==2699== 
    ==2699== Conditional jump or move depends on uninitialised value(s)
    ==2699==    at 0x400A18: mymerge (bar.c:118)
    ==2699==    by 0x400642: main (bar.c:19)
    ==2699== 
    ==2699== Conditional jump or move depends on uninitialised value(s)
    ==2699==    at 0x400A65: mymerge (bar.c:125)
    ==2699==    by 0x400642: main (bar.c:19)
    ==2699== 
    ==2699== Conditional jump or move depends on uninitialised value(s)
    ==2699==    at 0x400A8B: mymerge (bar.c:127)
    ==2699==    by 0x400642: main (bar.c:19)
    ==2699== 
    1 1 1 1 1 
    
    ==2699== 
    ==2699== HEAP SUMMARY:
    ==2699==     in use at exit: 0 bytes in 0 blocks
    ==2699==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
    ==2699== 
    ==2699== All heap blocks were freed -- no leaks are possible
    ==2699== 
    ==2699== For counts of detected and suppressed errors, rerun with: -v
    ==2699== Use --track-origins=yes to see where uninitialised values come from
    ==2699== ERROR SUMMARY: 11 errors from 8 contexts (suppressed: 4 from 4)
    I see a lot of for loops beginning at 1, which is just plain wrong for accessing arrays.
    Subscripts start at zero.
    rags_to_riches likes this.
    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.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Before Elysia says it: Don't remove parameter names in function prototypes.

    Pick a strategy for representing the range of values and stick to it. E.g. low is the first item in the range and high is one after the last item. The items in a range are [low, high) This strategy is the simplest and requires the least code.
    That makes your +1 here wrong:
    Code:
            mergesort(arr,mid+1,high);
    mid signifies one past the end of the first range which is also exactly the start of the next range
    Pay attention to the upper bound of your range everywhere in your program. Follow it all the way through, and you will get it working properly.
    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. Replies: 4
    Last Post: 11-10-2011, 06:00 PM
  2. Replies: 6
    Last Post: 11-05-2010, 11:34 AM
  3. Replies: 14
    Last Post: 11-17-2008, 12:31 PM
  4. Replies: 3
    Last Post: 05-23-2007, 12:42 AM
  5. Program failure
    By cnoob in forum C++ Programming
    Replies: 12
    Last Post: 01-14-2005, 12:34 PM

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