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 > Large Arrays in C++?

Large Arrays in C++?
Thread Tools
Posting Junkie
Join Date: Jun 2001
Location: Washington DC
Status: Offline
Reply With Quote
Feb 16, 2002, 04:17 PM
 
For some reason when I try to declare an array of 234,936 strings in C++ on OS X I get a seg fault. When i do it in Linux it works fine. Why is it doing this? Is there some way I can tell OS X not to?
     
Junior Member
Join Date: Apr 2000
Location: San Francisco, CA
Status: Offline
Reply With Quote
Feb 16, 2002, 08:54 PM
 
Originally posted by nonhuman:
<STRONG>For some reason when I try to declare an array of 234,936 strings in C++ on OS X I get a seg fault. When i do it in Linux it works fine. Why is it doing this? Is there some way I can tell OS X not to?</STRONG>
Are you declaring the array on the stack? If so, that's why. I'm surprised Linux survived it too (though I am not too familitar with the details fo Linux). Always create very large memory blocks dynamically (using new). Then check for the allocation's success by ensuring your pointer is not NULL.

Michael Kamprath
--
Michael F. Kamprath
     
Posting Junkie
Join Date: Jun 2001
Location: Washington DC
Status: Offline
Reply With Quote
Feb 16, 2002, 08:59 PM
 
Ok, thanks. I'll try that out.
     
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
Feb 17, 2002, 10:43 AM
 
Either that or change the size of the stack limitation (ulimit stack, for example).
     
Posting Junkie
Join Date: Jun 2001
Location: Washington DC
Status: Offline
Reply With Quote
Feb 17, 2002, 11:38 AM
 
Ok, stupid question: How do you declare an array dynamically? Is it
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>string* dictionary = new string[size]</font>[/code] or <BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>string* dictionary[size] = new string</font>[/code] or what?
     
Dedicated MacNNer
Join Date: Jan 2001
Location: Boulder, CO, USA
Status: Offline
Reply With Quote
Feb 17, 2002, 02:55 PM
 
string* dictionary = new string[size];

did that not work?

if you don't have to use a bare array (if this isn't homework or something), i'd suggest an stl container. std::vector or some such. they're really easy to use once you get the hang of them.

oh, and std::string, of course, is an stil container... is that what you're using?
     
   
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 12:42 PM.
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