ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1001. Reverse Root

Minimum memory allocation?
Posted by Dizzy 6 Dec 2008 04:18
Why does dynamic array (double *a = new double [256*1024*4]) work but double a[256*1024*4] doesn't?
Or more exactly why  does it inflict stack overflow?
Does it allocate more memory  or maybe use another memory?

Edited by author 06.12.2008 06:52
Re: Minimum memory allocation?
Posted by Dizzy 6 Dec 2008 10:19
I found!
________________________
double a[256*1024*4];
void main(){
...
}
________________________
will work, but
void main(){
double a[256*1024*4];
...
}
won't! Stack overflow!
Can anybody add somthing else?

Edited by author 06.12.2008 10:28
Re: Minimum memory allocation?
Posted by Anisimov Dmitry (Novosibirsk STU) 6 Dec 2008 21:40
Local variables are allocated on the stack,
new double[] allocates from the heap,
global varitables are allocated.. well.. um... not on the stack ;-)

You can increase stack size by using pragmas or
(more portable)
extern int _stklen=8888888;
Read FAQ also.
Re: Minimum memory allocation?
Posted by Anisimov Dmitry (Novosibirsk STU) 6 Dec 2008 21:54
update: _stklen is nonportable too.
Re: Minimum memory allocation?
Posted by Bohdan_prisoner 13 Dec 2008 03:40
)))
Third type of variables(global) are allocated in the memory called static...

And if you want you to write excelent programs on C++, you must to learn a lot about memory allocation ))

And as always pieces of advice: (off-top)
read Richter Jeffrey, Bruce Ekkel, Nikolai Jossatis, Aleksandresku - and problems in C++ will disappear.  And you'll have algorithm-making problem only )))