Why does this map crash VS?

Hi!
I'm experimenting with C++ in visual studio and am writing a message encrypter/decrypter

I'm using a map and an array to translate the numerical values of each letter, but using a map crashes the debugger.

I call Setup() when the code starts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 void Setup()
{
	chartoint->insert(std::make_pair('a', 1));
	chartoint->insert(std::make_pair('b', 2));
	chartoint->insert(std::make_pair('c', 3));
	chartoint->insert(std::make_pair('d', 4));
	chartoint->insert(std::make_pair('e', 5));
	chartoint->insert(std::make_pair('f', 6));
	chartoint->insert(std::make_pair('g', 7));
	chartoint->insert(std::make_pair('h', 8));
	chartoint->insert(std::make_pair('i', 9));
	chartoint->insert(std::make_pair('j', 10));
	chartoint->insert(std::make_pair('k', 11));
	chartoint->insert(std::make_pair('l', 12));
	chartoint->insert(std::make_pair('m', 13));
	chartoint->insert(std::make_pair('n', 14));
	chartoint->insert(std::make_pair('o', 15));
	chartoint->insert(std::make_pair('p', 16));
	chartoint->insert(std::make_pair('q', 17));
	chartoint->insert(std::make_pair('r', 18));
	chartoint->insert(std::make_pair('s', 19));
	chartoint->insert(std::make_pair('t', 20));
	chartoint->insert(std::make_pair('u', 21));
	chartoint->insert(std::make_pair('v', 22));
	chartoint->insert(std::make_pair('w', 23));
	chartoint->insert(std::make_pair('x', 24));
	chartoint->insert(std::make_pair('y', 25));
	chartoint->insert(std::make_pair('z', 26));
}

and here's the definition
 
std::map<char, int>* chartoint;

Why does this happen?
chartoint is an uninitialized pointer that points to some random place in memory. You're treating that random place in memory as if it is an object of type std::map<char, int> when it is not.
How would I fix this then?
I'm relatively new to C++ and come from a Java background, sorry for the noobiness
Don't use a pointer unless the semantics of your program require it.

1
2
3
4
5
6
7
8
#include <map>

std::map<char, int> chartoint = {
    {'a', 1},
    {'b', 2},
    {'c', 3},
    // ...
}; 




or, if you prefer the Setup route:
1
2
3
4
5
6
7
8
9
10
11
12
13
std::map<char, int> chartoint;

void Setup(std::map<char,int>& m)
{
    for (int i = 0; i < 26; ++i)
        m['a' + i] = i + 1;
}

int main()
{
    Setup(chartoint);
    // do stuff
}
Ah!
That first solution is just what I was after (I hate doing setup functions)

Thank You!
There is no need for a pointer. Just use a normal map.
std::map<char, int>chartoint;

There is also a much easier way to initialize your map.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <cstdlib>
#include <map>

using namespace std;

int main(void)
{
  std::map<char, int>chartoint;

  for (char ch = 'a', i = 1; i <= 26; i++, ch++)
  {
    chartoint[ch] = i;
  }
   
  for (auto it = chartoint.begin(); it != chartoint.end(); ++it)
  {
    std::cout << it->first << " = " << it->second << "\n";
  }
  system("pause");
  return EXIT_SUCCESS;
}

I've now got code similar to the code that cire provided, but I get this problem:
 
'initializing': cannot convert from 'initializer list' to 'std::map<char,int,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>> *'

and
 
The initializer contains too many elements


What's the problem now? it should work
I guess you use an older version of Visual Studio. These initializer lists are available since 2012(?) 2013.
I downloaded VS on my new PC about 2 weeks ago, so I presume its the 2016 version.

Here's the whole segment of code if this helps:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
std::map<char, int>* chartoint = {
	{'a',1 },
	{'b',2 },
	{'c',3 },
	{'d',4 },
	{'e',5 },
	{'f',6 },
	{'g',7 },
	{'h',8 },
	{'i',9 },
	{'j',10 },
	{'k',11 },
	{'l',12 },
	{'m',13 },
	{'n',14 },
	{'o',15 },
	{'p',16 },
	{'q',17 },
	{'r',18 },
	{'s',19 },
	{'t',20 },
	{'u',21 },
	{'v',22 },
	{'w',23 },
	{'x',24 },
	{'y',25 },
	{'z',26 }
};
This should work - without the asterik.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
std::map<char, int> chartoint = 
{
	{'a',1 },
	{'b',2 },
	{'c',3 },
	{'d',4 },
	{'e',5 },
	{'f',6 },
	{'g',7 },
	{'h',8 },
	{'i',9 },
	{'j',10 },
	{'k',11 },
	{'l',12 },
	{'m',13 },
	{'n',14 },
	{'o',15 },
	{'p',16 },
	{'q',17 },
	{'r',18 },
	{'s',19 },
	{'t',20 },
	{'u',21 },
	{'v',22 },
	{'w',23 },
	{'x',24 },
	{'y',25 },
	{'z',26 }
};
Aww, forgot I had the asterisk there, thanks!
Topic archived. No new replies allowed.