Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 19th, 2016, 11:34 PM   #1
colt
Programmer
 
Join Date: Mar 2005
Posts: 34
Rep Power: 0 colt is on a distinguished road
Maximum char array size

Hello I am trying to declare a char array of size of approximately nineteen million of positions and I am receiving a Segmentation fault when running my program. The declaration is the last thing of the program.

So what it is the maximum size I can use?
Should I use dynamic memory allocation? Or maybe a vector?
In this case what is the better option. I know in advance the size of the array, that's why I tried to use one and not a vector.


EDIT: I made I mistake, I only needed an array of 1/8 that size, and it worked just fine with this new size. But for future use, my question is the same. What is it the maximum size, should I use dynamic memory or a vector?
colt is offline   Reply With Quote
Old Apr 20th, 2016, 8:55 AM   #2
Smjprogrammer
Learning Very Basic Stuff
 
Smjprogrammer's Avatar
 
Join Date: Jan 2010
Location: United States
Posts: 978
Rep Power: 8 Smjprogrammer is on a distinguished road
Re: Maximum char array size

Have you tried something as simple as max_size to find out?
__________________
HP Laptop / AMD A4 - 3305 APU with Radeon HD Graphics 1.90 Ghz
Windows 10 64-Bit
Visual Studio Community 2015
Recommended Books - C++ All-In-One For Dummies / Webster's Computer Dictionary
Smjprogrammer is offline   Reply With Quote
Old Jun 1st, 2016, 4:29 PM   #3
sixstringartist
Programming Guru

 
Join Date: Jun 2005
Posts: 1,333
Rep Power: 13 sixstringartist will become famous soon enough
Re: Maximum char array size

How are you allocating this? Can you post some code?

If you're doing something like:
void func1()
{
    char myarray[19000000];
...

You're going to blow past the maximum stack size for your system (which is usually between 1 and 8MB)
Any variable you declare in this manner will be put on the stack with the exception of many STL containers whose internal data is dynamically allocated even if the container itself is function scoped.

Use STL/new/malloc for large data structures.
sixstringartist is offline   Reply With Quote
Old Jun 17th, 2016, 1:58 AM   #4
colt
Programmer
 
Join Date: Mar 2005
Posts: 34
Rep Power: 0 colt is on a distinguished road
Re: Maximum char array size

Quote:
Originally Posted by sixstringartist View Post
How are you allocating this? Can you post some code?

If you're doing something like:
void func1()
{
    char myarray[19000000];
...

You're going to blow past the maximum stack size for your system (which is usually between 1 and 8MB)
Any variable you declare in this manner will be put on the stack with the exception of many STL containers whose internal data is dynamically allocated even if the container itself is function scoped.

Use STL/new/malloc for large data structures.
Thanks for recommendations. Yes, that's precisely what I was doing. Fortunately I needed only about 2.25MB so the array was enough.
colt is offline   Reply With Quote
Old Jun 20th, 2016, 10:01 AM   #5
FlakkenTime
Software Developer
 
FlakkenTime's Avatar
 
Join Date: Apr 2010
Location: Bay Area, CA
Posts: 268
Rep Power: 7 FlakkenTime is on a distinguished road
Re: Maximum char array size

At 19 million bytes thats 19 MB. Note thats assuming its only a byte variable. An integer for example is 4 bytes so would have been 4 times the size. Having said that I don't know enough C++ to answer your question. My assumption is it should be able to handle that just fine. Quick google found this and it lists ways to check it:http://stackoverflow.com/questions/2...gth-limit-in-c
FlakkenTime is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Declaring File Input Array Size in Structures TellMeWhy C++ 3 Feb 19th, 2009 8:06 PM
Doubling the size of an array IceCold19 C 15 Sep 16th, 2007 4:40 AM
problem processing file into a char array csrocker101 C++ 1 May 8th, 2007 11:50 PM
changing size of an array Eric the Red Java 3 Apr 3rd, 2006 8:19 PM
Change the size of a character array dynamically? bivhitscar C 23 Nov 5th, 2005 5:55 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:00 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC