Solved

maximum limit of array size

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

770 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