How to restrict access to a foreign memory (C++, MS Visual Studio)?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
16 views
Let's say you have an array int * arr = new int[3]
When you try to write, say, arr[5], the compiler, of course, does not do it. But here is an attempt to read the value from there without problems. Is it possible to limit even attempt to read the value from the outside? (E.g. throwing an exception)
by | 16 views

1 Answer

0 like 0 dislike
Yes, you can. But to have to use a std::vector or std::array, the method at(). See the documentation, it says how.
By the way, the compiler will not prevent you from writing outside the array boundaries. This is not Rust. But it is UB in C++ and to do so not worth it.
by

Related questions

0 like 0 dislike
3 answers
0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
0 like 0 dislike
4 answers
0 like 0 dislike
1 answer
asked May 22, 2019 by JavaBeginner58
110,608 questions
257,186 answers
0 comments
35,406 users