As far as I can tell, none of these answers are correct. What am I missing?

When compiling some test questions for interviews, I am currently taking examples from various sources and reviewing them to assess their level of complexity and correctness. I came across something that I think is broken, but it is also possible that I missed something: if I, I want to know not only for my own knowledge, but it will also indicate that it is potentially useful, tricky question. I would like you to help me restore my sanity and reaffirm the trust that I have placed in myself .: D

What is the correct way to overlay p on the placeholder "???" in the following code?

#include <iostream> using namespace std; uint16_t hash(void *p) { uint32_t val = ???; return (uint16_t)(val ^ (val >> 16)); } int main(int argc, char *argv[]) { uint32_t a[20]; for(uint32_t i = 0; i < 20; ++i) { a[i] = i; cout << hash(a + i) << endl; } } 

Choose one:

  • static_cast<uint32_t>(p)
  • dynamic_cast<uint32_t>(p)
  • reinterpret_cast<uint32_t>(p)
  • const_cast<uint32_t>(p)

Ignoring for a moment that the hash call should be ::hash to ensure ambiguity with the standard library (for example, this line does not compile for me in GCC 5.3.0, C ++ 14 mode), I have a problem with the question.

Firstly, it is not clear what the program should do. Hash array values ​​or hash locations of elements? Since the function currently receives pointers to elements, but all available answers assume that these pointers themselves must be converted to uint32_t and used as a hash value. If so, then even if you use reinterpret_cast , then an error occurs because sizeof(void*) may not be sizeof(uint32_t) ; val in this function should be intptr_t instead . Using uint32_t for the type of the array elements themselves just confuses the questions further if this is a joint fall efficiently.

Or the function must have a hash value, and the correct answer is not in the list: *static_cast<uint32_t*>(p) .

The "correct" answer is apparently reinterpret_cast<uint32_t>(p) , which makes me think that the target of the program is the hash address of the array.

Do I represent these issues?
Is the question clear and is the solution one of the four proposed options?

+6
source share
2 answers

Just to summarize all the questions you made in the question and comments. Basically, the answer to this question is probably the following:

The right choice to use is reinterpret_cast 1 & dagger; & Dagger; .

This is a lot of footnotes! I guess this makes it a real C ++ question. In any case, I would not ask about this in an interview - this is a trick question too much - and there are many real questions with real, direct answers that you can ask instead.


1 On a 64-bit system, you cannot any_kind_of_cast a void* until uint32_t . So they are all wrong.

& dagger; There is the problem of finding a name in C ++ 11 between the hash() provided by the user and the class std::hash<T> . You should obviously associate them with why using namespace std; considered bad practice?

& Dagger; Even if there were no problems above that prevented the compilation of the code in the first place, is the goal is to actually hash the pointers to a rather than the values ​​of a ? The most likely answer should be:

 auto val = *static_cast<const uint32_t*>(p); 

If it comes to hashing pointers, then reinterpret_cast will be correct, but not uint32_t (according to (1)). We would like to use uintptr_t :

 auto val = reinterpret_cast<uintptr_t>(p); 

In the case of 64-bit code, you probably want to do something than val ^ (val >> 16) , since you are actually ignoring the upper 32-bit ones, but at least this will lead you to the correct execution, which compiles everywhere .

+5
source
  • IMHO, you imagine these problems :) An example was taken from here: reinterpret_cast Therefore it is better to look at an example in the context of this article.

  • The question is clear, but when I finished reading it and all the comments, I began to doubt my 1 minute decision to use reinterpret_cast (I would use it anyway as an answer even without context)

+4
source

All Articles