# What are the tradeoffs of using statically allocated arrays vs. pointers and dynamic allocation? [closed]

I am learning Monte Carlo simulation by C++. I begin with reading codes (from the internet and text books) of the 2D Ising model and the XY model. I find some people define spins simply by a two dimensional array int s[i][j], however, some people use vectors like int *s or int **s together with new int. The body of the code are also a bit different when allocating the value to spins, and I think the case only works with a two dimensional array is more readable.

It may be a quite naive to ask, but can anyone help me with what are advantages to use vector and new int type variable? References are also warmly welcome.

• I made a pretty major edit to your question title to use the proper terminology for this topic. The key difference here is static versus dynamic allocation. You will find many questions on StackOverflow about this and can get a lot more information elsewhere by searching with those keywords. – Doug Lipinski Dec 17 '14 at 3:39
• @DougLipinski Hi, thanks a lot. I am a newer in programming, probably my question is quite unprofessional. – R.Wigner Dec 17 '14 at 9:34
• It's not "unprofessional" at all, unfortunately it's just not a question that fits this site's topic very well. Please come back and ask more questions if they're specifically related to scientific programming applications! – Doug Lipinski Dec 17 '14 at 12:15

• It's also crucial to note the need to manually deallocate dynamically allocated arrays using the delete operator before they go out of scope. Failing to do so creates memory leaks. This is a major advantage of statically allocated arrays which are automatically deallocated upon exit of scope. – Doug Lipinski Dec 17 '14 at 3:41