vendredi 20 juillet 2018

Why does dereferencing a NULL unique_ptr NOT cause a segmentation fault? [duplicate]

This question already has an answer here:

I am familiar with C++ pointers and know that dereferencing them should cause segmentation faults but for my application, the only solution for a problem I was having would have been to in some cases dereference a null pointer. I wrote it in and then, boom, it strangely worked. Below is my code which compiles and runs fine (as far as I can tell).

//declaring the pointer to NULL then passing it
std::unique_ptr<opentracing::Span> span_1 = NULL;
foo(a, *span_1);

and for context the parameters of the foo function look like:

foo(int a, const opentracing::Span& parent_span){...}  

Is this safe to roll out for regular use?? Why does this work? I am very confused and would like any advice whether it's ok to keep this fix in my code. Thanks!

Aucun commentaire:

Enregistrer un commentaire