I am delving into C++ as of today, and would like to know what risk I am putting my computer at. Does Xcode prevent me from breaking my stuff if I mess up on a bit of array math somewhere? Like every other C++ compiler I know of, it doesn't catch errors of that nature at compile time. Does it perhaps allocate a safe amount of memory to my program during testing so I don't rewrite any critical memory by chance?
-
1Your C++ process won't be allocated an unbound amount of memory. That's a kernel level operation - you'd have to manually allocate that amount of memory since C++ won't dynamically resize your heap or stack without you putting in the work AFAIK. – theWanderer4865 Sep 10 '15 at 17:29
-
2Xcode isn't a compiler, it's an IDE that (among other things) drives some compiler... which compiler depends on your version of Xcode, but it's probably the clang compiler. Beyond that, the extra memory some environments allocate is not to protect against boundary overflows (and relying on them for such is going to lead to trouble eventually)... it's to make it possible to detect them (by observing that memory which should be a certain value is no longer that value) - but this detection is not real-time generally. – mah Sep 10 '15 at 17:31
-
Beware ye all who enter herein. C++ is not for cry-babies. Bring a hard hat and some decent wellies. – Galik Sep 10 '15 at 17:33
2 Answers
Xcode is an IDE, the compiler job is delegated to someone else, in this case, g++ or clang++. Neither of them do any bound checking (at least not by default), but...
Any modern OS (this even includes Mac OS X), thanks to virtual memory mechanism will prevent any damage to system or other stuff - the worst you can do with out-of-bounds array access is crash your own program.
If you're extra paranoid, you can run your program inside virtual machine. There are free programs available for this purpose.

- 15,379
- 3
- 47
- 71
Does XCode prevent you from writing bad code? It has some static analysis: https://developer.apple.com/library/ios/recipes/xcode_help-source_editor/chapters/Analyze.html
Can index out of bounds on a c++ array cause physical harm to your computer? Extremely unlikely, but not impossible I suppose: https://security.stackexchange.com/questions/65153/is-there-any-virus-that-can-cause-physical-damage
No compiler you know of that check c++ array bounds? If you want array bounds checking, don't use a c++ array, use std::vector at()
Can your c++ array write overwrite critical memory? That's a function of your kernel vs virtual memory space.