Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 07-27-2006, 09:57 PM   #1
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:
#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];


  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, 10:40 PM   #2
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, 10:46 PM   #3
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, 06:47 AM   #4
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


for 8Mb of stack memory.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

All times are GMT -5. The time now is 10:48 AM.

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