Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

void* and temporaries

$
0
0
I was reading online and someone suggested doing something along the following lines (this is a simplified example of the mechanic, not a real-world example):

#include <iostream>

int main()
{
  int a = 5;
  int b = 10;
  void *p = reinterpret_cast<void*>(a * B)/>;
  int result = *reinterpret_cast<int*>(&p);
  std::cout << std::endl << result << std::endl;
  return 0;
}


I want to know whether this is considered a safe operation, and why it works.

What is the pointer p pointing at, and why do I have to take the address of the pointer before I can dereference it?

As a follow-up I'm interested by the implications of a similar method in terms of function pointers, but first I need to understand what's happening here, whether it's safe, portable, etc.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>