Reputation: 105
I have two version of the code using maps
library in C++:
Version 1
map<string, int> *trash;
cout << "inserting" << endl;
trash->insert(pair<string,int>("hello",12));
cout << "inserted" << endl;
Output of version 1:
inserting
Version 2
map<string, int> trash;
cout << "inserting" << endl;
trash["hello"] = 12;
cout << "inserted" << endl;
Output of version 2:
inserting
inserted
I am not sure why version 1 is unable to insert. I need to use version 1 only (as the map trash
has to be a pointer in my program).
Note: there are no compilation errors in both codes.
Upvotes: 0
Views: 1568
Reputation: 297
You need to allocate memory in heap so that memory can be created for map
map<string, int> *trash = new (std::nothrow) map<string, int>();
if(NULL==trash)
{
std::cout<<"Bad allocation"<<endl;
return errcode;
}
Upvotes: 4
Reputation: 652
You should initialize your pointer, make it point to somewhere. It does not have syntax error, so it can compile, but when you put data to somewhere you did not initialize, the operation system will block the operation, so the program will crash. I think you can change your version 1 code like this:
map<string, int> *trash = new map<string, int>();
cout << "inserting" << endl;
trash->insert(pair<string, int>("hello", 12));
cout << "inserted" << endl;
This will allocate memory for your pointer in the heap;
Or you can also do like this:
map<string, int> trash_origin;
map<string, int> *trash = &trash_origin;
cout << "inserting" << endl;
trash->insert(pair<string, int>("hello", 12));
cout << "inserted" << endl;
This will allocate memory for you pointer in the stack. I hope this will help you!
Upvotes: 0
Reputation: 904
Here is your working program link: http://ideone.com/rqa9tC
#include <iostream>
#include <map>
using namespace std;
int main() {
map<string, int> *trash; // creating * variable which doesn't have place to hold your values
trash = new map<string, int>(); // creating memory and assigning to empty variable
cout << "inserting" << endl;
trash->insert(pair<string,int>("hello",12)); // now it can hold your values
cout << "inserted" << endl;
delete trash;
return 0;
}
and about your second case, compiler was doing this for you.
Upvotes: 1
Reputation: 40336
You have declared a pointer, but you do not initialize it to anything and do not instantiate a new map
:
map<string, int> *trash;
Its initial value is random. You then attempt to dereference that uninitialized pointer:
trash->insert(pair<string,int>("hello",12));
All bets on behavior are off at that point. You'd want to make sure you actually create a new map first:
map<string, int> *trash = new map<string, int>();
Your code does not produce a compilation error because, syntactically, it is correct, even though the behavior is not correct. Some compilers will generate warnings if you attempt to use uninitialized variables like that, though. You may want to see if you can adjust the warning level of whatever compiler you are using to help you catch things like this in the future.
Upvotes: 2
Reputation: 206627
You haven't allocated memory for the pointer. You are seeing undefined behavior.
map<string, int> *trash = new map<string, int>();
should fix it.
Upvotes: 3