1

Why does the maximum array varies from local declaration and global declaration?

I have read in some blog that maximum size of array that we can declare would be 10^6 in case of local declaration and between 10^7 to 2*10^8 in case of global declaration. What is the reason for this?

1
  • 4
    I tend to avoid those kind of blogs. Commented Feb 17, 2014 at 9:58

2 Answers 2

4

Those numbers don't mean a thing in general, they are specific to the OS + Machine. But local declaration and global declaration are done in different regions of virtual memory. The local variables are on the stack, which is generally smaller than the heap (used for dynamically allocated global variables)

Sign up to request clarification or add additional context in comments.

6 Comments

will there be a limit as such for dynamic arrays
Yes, the amount of ram memory available.
@UmNyobe Some 32-bit platforms may limit the size of one object (either statically or dynamically allocated) to 2GiB even if more memory is available to the process (and can be allocated as several objects) in order to prevent pointer differences that overflow a ptrdiff_t defined as a 32-bit signed integer.
thanks @PascalCuoq. it is more accurate to say that it depends on the memory allocator.
@UmNyobe so do you mean can we allocate dynamic arrays that could make it to size of GiBs
|
0

Global declaration reserves memory on the static memory which is large enough to not impose a memory problem. But local declarations reserves memory on the stack which it's memory is limited. So you can't declare an array as a local object with a large amount of memory otherwise you get out of the memory range which is known as a Stack Overflow

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.