Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Basic XCode/C++ array question

Basic XCode/C++ array question
Thread Tools
Fresh-Faced Recruit
Join Date: May 2002
Status: Offline
Reply With Quote
Jul 6, 2005, 02:36 PM
 
Hello all. I am running XCode 1.5 on a PowerBook G4. I need a very large array, as part of my research code. When I attempt to run the following stripped-down code

#include <iostream>
int main (int argc, char * const argv[]) {
double x[250000];
return 0;
}

I get an error from Xcode saying "Executable has exited due to signal 11 (SIGSEGV)," a segmentation fault I think. If I decrease the size of the array to 100,000, then the array can be declared and the program runs. What can I do to declare a larger array? Thanks.
     
Mac Enthusiast
Join Date: Nov 2003
Status: Offline
Reply With Quote
Jul 6, 2005, 02:43 PM
 
Originally Posted by jhs2n
Hello all. I am running XCode 1.5 on a PowerBook G4. I need a very large array, as part of my research code. When I attempt to run the following stripped-down code

#include <iostream>
int main (int argc, char * const argv[]) {
double x[250000];
return 0;
}

I get an error from Xcode saying "Executable has exited due to signal 11 (SIGSEGV)," a segmentation fault I think. If I decrease the size of the array to 100,000, then the array can be declared and the program runs. What can I do to declare a larger array? Thanks.
You're blowing the stack; you should keep large arrays in the heap by allocating the memory dynamically:

Code:
double* x = (double*) malloc(250000 * sizeof(double)); if(x) { ... free(x); // when done }
or

Code:
double* x = new double[250000]; if(x) { ... delete[] x; // when done }
     
jhs2n  (op)
Fresh-Faced Recruit
Join Date: May 2002
Status: Offline
Reply With Quote
Jul 6, 2005, 02:51 PM
 
Thanks so much. The latter code did the trick.
     
   
Thread Tools
Forum Links
Forum 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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:20 AM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2