LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-04-2005, 11:57 PM   #1
oulevon
Member
 
Registered: Feb 2001
Location: Boston, USA
Distribution: Slackware
Posts: 438

Rep: Reputation: 30
Segmentation fault after declaring a large array.


This program has a segmentation fault as it enters the for loop in the estimatepi function. Does anyone know why? I think it might have to do something with the array. Anyone have any ideas. Thanks for any help.



Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAXARRAY 50000000
double estimatepi(int n); 

int main (int argc, char * argv[]){

  double MyPi = 0.0;
  printf("I started with %f.\n", MyPi);  

  MyPi = estimatepi(10000000);
  

  
  return(0);
}

double estimatepi( int n ){
  
   int i;
   int count = 0;
  double numpair[MAXARRAY][2];
  
 
  for (i = 0; i < n; i++){ 
    numpair[i][0] = ((double) rand())/RAND_MAX;
    numpair[i][1] = ((double) rand())/RAND_MAX;
  }
  
     for (i = 0 ; i < n; i++){ 
       if( ((numpair[i][0] * numpair[i][0]) + (numpair[i][1] * numpair[i][1])) < 1.0) count++;
     }
     
     return((((double)count)/n) * 4.0);

}
 
Old 11-05-2005, 12:23 AM   #2
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
Are you aware you are declaring an array of size 781.250MB (in a pc )!!!
 
Old 11-05-2005, 12:31 AM   #3
zhangmaike
Member
 
Registered: Oct 2004
Distribution: Slackware
Posts: 376

Rep: Reputation: 31
I'm betting your array is too huge... but, looking at your code, you don't need an array anyway - just combine your loops and remove the array:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAXARRAY 50000000
double estimatepi(int n); 

int main (int argc, char * argv[]){

  double MyPi = 0.0;
  printf("I started with %f.\n", MyPi);  

  MyPi = estimatepi(10000000);
  

  
  return(0);
}

double estimatepi( int n ){
  
   int i;
   int count = 0;

// This isn't needed.
//  double numpair[MAXARRAY][2];
  
 
  for (i = 0; i < n; i++){ 
    /*numpair[i][0]*/
    double a = ((double) rand())/RAND_MAX;
    /*numpair[i][1]*/
    double b= ((double) rand())/RAND_MAX;

    // Code from old loop moved here
       if( ((a*a) + (b*b)) < 1.0) count++;

  }
       
     return((((double)count)/n) * 4.0);

}
 
Old 11-05-2005, 12:33 AM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Works fine for me:

(gdb) p MyPi
$2 = 3.14113

Suggestions:

1. Compile with -g, step into the code with gdb, and troubleshoot with the debugger
2. Compile with -S, and look at how the stack is being allocated (it should be exactly what you're expecting)
3. Compile with different combinations of gcc "-fstack-check", " -fstack-limit-symbol" and/or "-fno-stack-limit"
 
Old 11-05-2005, 12:37 AM   #5
zhangmaike
Member
 
Registered: Oct 2004
Distribution: Slackware
Posts: 376

Rep: Reputation: 31
...you can still get the same result without arrays, and without using close to a gig of memory.
 
Old 11-05-2005, 12:45 AM   #6
oulevon
Member
 
Registered: Feb 2001
Location: Boston, USA
Distribution: Slackware
Posts: 438

Original Poster
Rep: Reputation: 30
Thanks for the help guys. It's not my program. I was trying to determine the seg fault for someone else. I assumed it was the array size, and I had run it through gdb already, but it still seg faults.
 
Old 11-08-2005, 02:41 AM   #7
elluva
Member
 
Registered: Aug 2003
Location: Belguim, Ostend and Ghent
Distribution: Ubuntu
Posts: 600

Rep: Reputation: 30
If an array is declared that is too large, I think you will get a null pointer. So you might insert some code to check that pointer.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika SUSE / openSUSE 2 12-02-2005 09:34 AM
C++: Trouble declaring dynamic array Hady Programming 7 11-01-2005 01:06 AM
Problem Accessing Large RAID Array FlyingPenguin128 Debian 1 05-16-2005 10:51 PM
segmentation fault when array size exceed 1GB ymei Programming 14 11-11-2003 10:27 AM
mkfs and mounting large (1.4TB) hardware raid array. Frustin Linux - General 1 05-20-2002 06:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration