R
R
rsatarov2018-05-04 15:58:10
C++ / C#
rsatarov, 2018-05-04 15:58:10

How to restrict access to someone else's memory (C++, MS Visual Studio)?

Suppose there is an array int * arr = new int[3]
When you try to write, say, in arr[5], the compiler, of course, does not allow you to do this. But here's an attempt to read the value from there - no problem. Is it possible to somehow restrict even an attempt to read the value from the outside? (For example, throwing an exception)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly, 2018-05-04
@vt4a2h

Yes, you can. But for that you need to use std::vector or std::array, at() method. See the documentation, it says how.
By the way, the compiler will not prevent you from writing beyond the boundaries of the array. This is not Rust for you. But this is UB in C++ and you shouldn't do that.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question