allocation

Static array vs. dynamic array in C++

被刻印的时光 ゝ 提交于 2019-11-25 23:37:17
问题 What is the difference between a static array and a dynamic array in C++? I have to do an assignment for my class and it says not to use static arrays, only dynamic arrays. I\'ve looked in the book and online, but I don\'t seem to understand. I thought static was created at compile time and dynamic at runtime, but I might be mistaking this with memory allocation. Can you explain the difference between static array and dynamic array in C++? 回答1: Local arrays are created on the stack, and have

Getting a stack overflow exception when declaring a large array

為{幸葍}努か 提交于 2019-11-25 21:44:49
问题 The following code is generating a stack overflow error for me int main(int argc, char* argv[]) { int sieve[2000000]; return 0; } How do I get around this? I am using Turbo C++ but would like to keep my code in C EDIT: Thanks for the advice. The code above was only for example, I actually declare the array in a function and not in sub main. Also, I needed the array to be initialized to zeros, so when I googled malloc, I discovered that calloc was perfect for my purposes. Malloc/calloc also