5

Is there a safe way to create a pointer in C++ apart from nullptr that causes an access violation every time it is accessed?

One of my unsuccessful attempts is adding 1 to nullptr.

In my special case I need a solution for Visual Studio 2019.

Tobias
  • 5,038
  • 1
  • 18
  • 39
  • 3
    UB is UB, so from language point of view, no. If you specify compiler and option, there might have specific way. – Jarod42 Jun 08 '21 at 08:21
  • 1
    If you're looking for a "save" way to "crash" your application I would use [std::abort()](https://en.cppreference.com/w/cpp/utility/program/abort). - Btw. once when I was developing on SGI Irix I wasn't aware that 0 didn't cause a segmentation fault (as I was assuming). This was a bad surprise when I ported my code to Linux where I even wasn't able to enter `main()` before my application was aborted... – Scheff's Cat Jun 08 '21 at 08:24
  • 1
    Out of curiosity: What is the background of your question? Why can't you use the ```nullptr```? – FloWil Jun 08 '21 at 08:29
  • [`fullptr`](https://codingtidbit.com/2019/04/01/c22-fullptr-to-replace-nullptr/) :-) (see the date). – Jarod42 Jun 08 '21 at 08:32
  • @Jarod42 Oh wow that writing style is painfully unfunny. – Konrad Rudolph Jun 08 '21 at 08:33
  • @Jarod42 I added the compiler for my special case in the question (the C++ compiler used in Visual Studio 2019). – Tobias Jun 08 '21 at 08:44
  • @Scheff'sCat `std::abort()` is a good comment since others might find it useful. In my case it is not a solution. I will outline my use-case as far as I am allowed to in the answer to the comment from FloWil. – Tobias Jun 08 '21 at 08:48
  • 1
    @FloWil I am working on a large program that includes the analysis of code for some modeling language. There is an analysis phase where the special pointers I am discussing in the question indicate that certain simplifications are not possible. The special pointers may not leak out of that analysis phase. It is a program failure if they appear in the code that is finally generated and executed. `nullptr` is already used for not yet initialized pointers and cannot serve my purpose. It is also treated differently. – Tobias Jun 08 '21 at 09:01
  • @Jarod42 Thanks for the comment about `fullptr`. Even if it is intended as a joke it shows that others think about it. – Tobias Jun 08 '21 at 09:04
  • How about throwing exception? – Jarod42 Jun 08 '21 at 09:07
  • @Jarod42 Throwing an exception would require additional code in the finally generated code for the pointer access or an additional analysis step that scans the code for the special pointer. We actually want to avoid that extra stuff just for detecting the leaking of the special pointer value from the mentioned analysis step. Hence my question. (Especially extra code in the finally generated code is out of question.) – Tobias Jun 08 '21 at 09:14
  • 1
    If you are using MSVC, could you make use of any of the adresses mentioned here: https://stackoverflow.com/a/127404/9898968 I'm not sure if you are supposed to use those yourself though. – FloWil Jun 08 '21 at 09:29
  • I'm curious, how was adding 1 to `nullptr` unsuccessful for you ([it compiles fine for me](https://godbolt.org/z/Ga79GfdE4))? Also, I'd expect that dereferencing any address within the whole address space of page 0 under Microsoft Windows would have the same effect as referencing the very first address of that page. So dereferencing `static_cast(static_cast(nullptr)+1)` I'd expect to also cause a `STATUS_ACCESS_VIOLATION` but with `00000001` as the address that was attempted to be accessed. Which seems to provide what you're looking for albeit it's not specified per C++. – Louis Langholtz Feb 04 '23 at 04:31
  • I'm also curious if you've considered having MS Visual Studio use clang and writing your own [LLVM backend](https://llvm.org/docs/WritingAnLLVMBackend.html) for it which checked pointer dereferences and then provided alternative behavior for dereferences of non *safely-derived* pointers that's closer to your needs. Seems like one might also be able to achieve something similar writing ones own [Valgrind](https://valgrind.org) based tool. Sounds like a lot of work either way but an interesting project. – Louis Langholtz Feb 04 '23 at 05:30

0 Answers0