LinuxQuestions.org
Help answer threads with 0 replies.
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 07-27-2006, 08:57 PM   #1
ErrorBound
Member
 
Registered: Apr 2006
Posts: 280

Rep: Reputation: 31
Seemingly random combination of statements causes segmentation fault (C++)


Hi, I'm trying to figure out what's going wrong in a simulation I'm writing. It probably has something to do with the lot of big arrays. I can get it down to the following:
Code:
#include <iostream>
#include <ctime>
#include <cstdlib>

#define BIG 1000000

using namespace std;

int main()
{
  unsigned int myints[BIG];
  unsigned int myints2[BIG];
  unsigned int myints3[BIG];

  srand(time(NULL));

  rand() % 5; // can be any number here

  return 0;
}
As is, this causes a segmentation fault. However if any of the lines are removed or commented out, there are no errors. Also if you take one 0 off of BIG, there are no errors. I don't think I'm out of memory because I can add myints4[BIG], myints5[BIG], etc if the random numbers are removed.

I have no idea what's going on here..
 
Old 07-27-2006, 09:40 PM   #2
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
You are using too much memory on the stack. Maybe getting a random number somehow causes it to try to use memory on the stack, or something, which actually causes the segmentation fault; whereas maybe otherwise it doesn't try to use memory on the stack.
 
Old 07-27-2006, 09:46 PM   #3
ErrorBound
Member
 
Registered: Apr 2006
Posts: 280

Original Poster
Rep: Reputation: 31
So maybe I should just try sticking some of those arrays on the heap (free store)? And hope it goes away? (stack, heap, I can't keep these names straight)
 
Old 07-28-2006, 05:47 AM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
The loader ld.exe reserves 2Mb of stack, so with two arrays you come in just under the threshold, but with the third array you exceed the limit. This can be changed by using the --stack option of the loader. see man ld. However if you are linking through g++ then you will probably need to use the following switch

-Wl,--stack=0x0800000

for 8Mb of stack memory.
 
  


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
ip of my server changes, seemingly at random esteeven Linux - Networking 7 03-10-2007 08:18 AM
Random Segmentation Fault in Gyach-E, Suse 10 Seraph Linux - Software 3 12-06-2005 10:59 PM
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika SUSE / openSUSE 2 12-02-2005 09:34 AM
9.1 installation freezes at seemingly random points mustardseed Slackware - Installation 2 03-02-2004 05:25 PM
Seemingly random hostname changes xanthumn Linux - Networking 2 01-21-2003 03:30 AM

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

All times are GMT -5. The time now is 12:53 PM.

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