Solved

maximum limit of array size

Posted on 2006-11-23
4
625 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 50 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now