[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

maximum limit of array size

Posted on 2006-11-23
4
Medium Priority
?
669 Views
Last Modified: 2012-06-22
what is the maximum limit of array size ?
e.g. array[n] ... wats the size of n?

i guess it should also depend on the type of array?
kindly clearify the concept

regards
0
Comment
Question by:fatimao
  • 3
4 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 18006377
Essentially it's limited by the stack size.
The C++ language itself does not impose a limit afaik.
Apart from the machine limit of course : the size of an unsigned int - ie. the theoretical limit on a 32-bit machine would be 2^32 - 1. But you'd probably never get there anyway, because you'll hit memory constraints long before that :)

In any case, it's not good practice to create large static arrays (on the stack) - use dynamically allocated memory instead (on the heap).
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 200 total points
ID: 18006386
>> ie. the theoretical limit on a 32-bit machine would be 2^32 - 1
Let me clarify that. The theoretical limit for this array :

    T arr[N];

would be :

    N*sizeof(T) <= 2^32 - 1

or :

    N <= (2^32 - 1)/sizeof(T)

Again for a 32-bit machine with 32-bit unsigned ints.
0
 

Author Comment

by:fatimao
ID: 18012128
please can you tell what will be the limit of the stack size assigned to the process?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18012180
The stack is a part of the process memory used for statically allocated memory (for function calls, local variables, etc.). Its size is entirely implementation/platform dependent and is not prescribed by the C++ standard.

In any case, you shouldn't depend on any value for that size - that's not a safe way to code. If you get to the limit of the stack, your program will generate a stack overflow.

As I said : if you have a lot of memory to allocate, DON'T do it on the stack, but on the heap (ie. dynamically allocated).

Do you know what a stack and heap are, and how process memory is organised ?
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question