-2

I was testing std::unordered_maps to get used to them before actually using them in a project.

And I noticed that, if I use pointers as keys, I get a segfault error when the pointer used as the key actually points to a value. This is my code:

#include <unordered_map>
#include <stdio.h>

int main() {
    std::unordered_map<int*, int> map;

    int* key;
    //*key = 18;

    map[key] = 1893;

    printf("%d\n", map[key]);
}

Now, if I uncomment that commented line, I get a segmentation fault error, while if I leave it commmented, everything works perfectly. Why does this happen?

user6245072
  • 2,051
  • 21
  • 34
  • 1
    you forgot to allocate memory to the pointer. `int *key = new int` – Rohith R Sep 26 '16 at 06:31
  • The commented line dereferences an uninitialized pointer, how do you expect that to work? – πάντα ῥεῖ Sep 26 '16 at 06:32
  • Have a look at this [pointer tutorial](http://www.cplusplus.com/doc/tutorial/pointers/). This explains all you need to know, why this won't work. – muXXmit2X Sep 26 '16 at 06:45
  • "everything works perfectly" - I suppose that depends on how liberal your definition of "works perfectly" is. The value of `key` as presented in the code is *indeterminate*. Usage of the value of `key`, therefore, invokes *undefined behavior*. That the code doesn't "crash" doesn't mean it "works". It means you were (un)lucky that invoking undefined behavior didn't manifest as a crash. For more info, see: [Is reading an indeterminate value undefined behavior?](http://stackoverflow.com/questions/4279264/is-reading-an-indeterminate-value-undefined-behavior) – WhozCraig Sep 26 '16 at 06:51
  • The code should work without segfault, and it does work as expected on my Linux system, both in g++ and clang, even with -O3 optimization. Please check if what crashes is indeed your sample code and not some other program. – olpa Sep 26 '16 at 07:08
  • @olpa it works _with_ the comment, or _without_ the comment? – user6245072 Sep 26 '16 at 07:40
  • http://stackoverflow.com/questions/1597405/what-happens-to-a-declared-uninitialized-variable-in-c-does-it-have-a-value – Zan Lynx Sep 26 '16 at 07:46
  • 1
    Works when commented-out. And when the instruction `*key=18` is active, it must fail. The error has nothing to do with unordered_map, the error is the dereference of the unintialized pointer. To repair, use something like this: `int xxx = 18; int *key = &xxx`. – olpa Sep 26 '16 at 07:50
  • You have to assign `key` a value before you attempt to use its value! `*key = 18;` uses the value of `key` to know where to store the 18 -- except it has no value. – David Schwartz Nov 29 '16 at 07:14

1 Answers1

1

You declared a pointer, not an int. The pointer points to nowhere, as you did not initialize it. There is no place to write the 18 to, because the pointer doesn't point to any space.

Note that your issue is completely unrelated to unordered maps.
int * key; *key = 18; will dump in any context.

Use either
int key; key = 18;,
or if you really want key to be a pointer:
int * key = new int; key = 18;

Aganju
  • 6,295
  • 1
  • 12
  • 23