c++ - Set value of random memory address -


i understand isn't practice following. however, curious if possible?

x = 1; cout << *(&x + 1) << endl; // prints value @ next memory address. (&x + 1) = 0; // doesn't work.  // want set value of next memory address. 

the error says:

error: lvalue required left operand of assignment

is there can change make work, though understand isn't useful.

the error due trying assign temporary address.

(&x + 1) = 0; 

what meant was:

*(&x + 1) = 0; 

the results of expression depends on type of x. if x of type int, result in undefined behaviour assigning memory have not been allocated.

however, using type following result in defined behaviour example.

struct foo {     foo& operator=(int i) { x[0] = i; return *this; }     int* operator&() { return x.data(); }     std::array<int, 2> x{{0}}; };  int main() {     foo x;     x = 1;     std::cout << *(&x + 1) << std::endl; // equivalent 'x.x[1]' '0'.     *(&x + 1) = 0; // ok. } 

in c++, undefined behaviour access memory have not been allocated. results of such operation can result in pretty as, e.g., execution of random instruction laying around in deallocated memory.


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -