Solved

maximum limit of array size

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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. …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

791 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