473,395 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

why it is causing a stack fault..??

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2.       #include<conio.h>
  3.  
  4.       void mergesort(int [],int,int);
  5.       void merge(int [],int,int,int);
  6.  
  7.       void main()
  8.       {
  9.          int data[50],i,no_elements;
  10.          printf("\n enter no. of elements :");
  11.          scanf("%d",&no_elements);
  12.  
  13.          printf("\n enter elements:");
  14.  
  15.          for(i=0;i<no_elements;i++)
  16.          {
  17.             scanf("%d",&data[i]);
  18.          }
  19.  
  20.                  printf("\n entered array:");
  21.          for(i=0;i<no_elements;i++)
  22.          {
  23.             printf("%d",data[i]);
  24.          }
  25.  
  26.  
  27.          mergesort(data,0,no_elements-1);
  28.          printf("\n sorted list:");
  29.  
  30.          /*for(i;i<no_elements;i++)
  31.          {
  32.           printf("%d",*(data+i));
  33.          } */
  34.  
  35.       }
  36.  
  37.       void  mergesort(int a[],int i,int j)
  38.       {
  39.          int mid;
  40.          if(i>=j)
  41.          return ;
  42.          else
  43.          {
  44.           mid=i+j/2;
  45.  
  46.           mergesort(a,i,mid);
  47.           mergesort(a,mid+1,j);
  48.           merge(a,i,j,mid);
  49.          }
  50.       }
  51.  
  52.       void merge(int a[],int i,int j,int mid)
  53.       {
  54.           int arbitary[100],start1,start2,k,last;
  55.           start1=i;
  56.           start2=mid+1;
  57.           last=j;
  58.           k=i;
  59.         while(start1<=mid && start2<=last)
  60.             {
  61.           if(a[start1]<=a[start2])
  62.           {
  63.             arbitary[k]=a[start1];                      
  64.         k++;
  65.             start1++;
  66.     }
  67.     else
  68.         {
  69.         arbitary[k]=a[start2];
  70.     k++;
  71.     start2++;
  72.     }
  73.      }
  74.           if(start1>mid)
  75.           {
  76.              while(start2<=last)
  77.              arbitary[k++]=a[start2++];
  78.           }
  79.           else if(start2>j)
  80.           {
  81.              while(start1<=mid)
  82.              arbitary[k++]=a[start1++];
  83.           }
  84.       }
above program is for merge sort..

why it is causing a stack fault..??
Mar 18 '13 #1
2 1121
weaknessforcats
9,208 Expert Mod 8TB
Start using your debugger. Set some breakpoints in each of your functions to see how the program is executing.

Your merge function is part of a recursion and it creates an array on he stack. Do enough recursing and you could use up your allotted stack memory.

If that's the case, start using malloc() to shift memory management to the heap.
Mar 18 '13 #2
donbock
2,426 Expert 2GB
When you say "stack fault", what was the precise error message?

Following line 11 you should verify that 0 < no_elements <= 50. Undefined behavior will occur if no_elements is outside this range.
Mar 19 '13 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: sandwich_eater | last post by:
I get a segmentation fault in my program when calling a function "TestFn" that has been passed as a pointer into another function. The following excerpt should give enough information as to what I...
10
by: Saurabh | last post by:
Hi all, I am working on RedHat Linux GCC 3.0. I am trying to convert a long to string through sprintf. but i m getting segmantation fault. I tried snprintf also but no avail.here is the piece...
0
by: Mullai | last post by:
0Hi, My exe comes out with two types of errors like : 1.PG1609VV caused an invalid page fault in module KERNEL32.DLL at 017f:bff9dfff. Registers: EAX=07fbfe38 CS=017f EIP=bff9dfff...
1
by: freestyle12 | last post by:
Hey i've been trying to get this to work for a while now but nothing i do fixe it. causes a seg fault at strcat. Here is the code snippet: int i=1; char *opt = " "; char s = '-'; ...
27
by: Jess | last post by:
Hello, the following code failed with "segmentation fault". Could someone tell me why it failed please? Thanks! #include<iostream> #include<string> #include<cstring> #include<cstddef> using...
23
by: sam_cit | last post by:
Hi Everyone, I have the following program unit, #include <stdlib.h> int main() { char *p = (char*)malloc(100); if(p==NULL)
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.