 |
 |
Large Arrays in C++?
|
 |
|
 |
|
Posting Junkie
Join Date: Jun 2001
Location: Washington DC
Status:
Offline
|
|
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
|
|
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
|
|
Ok, thanks. I'll try that out.
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status:
Offline
|
|
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
|
|
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
|
|
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?
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|