By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,997 Members | 1,260 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,997 IT Pros & Developers. It's quick & easy.

Memory access error with STL composed types

P: 3
Hi all.
First of all let me apologize by my english.
I've googled a lot about my problem, but I had not find anything conclusive.
I have the following piece of code:

Expand|Select|Wrap|Line Numbers
  1. vector < map < int , char * > > bank;
  2. bank . reserve ( 10 );
  3. if ( bank [ 0 ] [ 0 ] == "a" ) //Memory access error (Access violation at address #blabla)
which give me the error indicated in comment.
So, to test, I included the following line before "if", and the error changes to that line

Expand|Select|Wrap|Line Numbers
  1. bank [ 0 ] [ 0 ] = "b"; //Same error
Finally, the following inserted in place of the line above, works.
Expand|Select|Wrap|Line Numbers
  1. map < int , char * > data;
  2. data [ 0 ] = "a";
  3. bank . push_back ( data );
But this is hardly an option to me. Mainly because I need, most of times, to test the variable "bank" (even the NULL address existence, which tells me that new item insertion is ok) before I can decide what to do. Beside that, that association grows interactively.
As I can see, it seems a problem with map allocation.
So, my exactly question is how can I use this type of association ( vector < map < type1 , type2 > > ) without get into Mermory access violation errors? Is there a way to previously initialize that association in a manner that allow me to insert new element just doing
Expand|Select|Wrap|Line Numbers
  1. bank [ i ] [ j ] = "some"
even after the limits previously defined (growing the association)?
Please excuse me if this is trivial, but I'm new to STL.
I appreciate any helps.
Thanx in advance.
Nov 15 '07 #1
Share this Question
Share on Google+
4 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
I see the vector named bank.

I do not see that this is a two-dimensional array so your bank[x][y] syntx just will not work.

If you are new to STL, then this vector is a very advanced one indeed for you to be using. Maybe you could tell me what youare trying to do. It may be you are doing things the hard way.
Nov 15 '07 #2

P: 3
In fact, I've change it a bit. What I have is vector < map < int , Object * > >, but I think it does'nt matter. What I need is exaclty what I try to represent.An array with each element having an associative array. To clarify: each position of the array represent a level. In each level I've a group of cells (of a grid) that I must inspect and I use their index as a key in associative array.
You say that bank [ i ] [ j ] does'nt look like a bidimensional array for you. But, in fact, I can get the array element value when I do var = bank [ i ] [ j ] (of course, when I use push_back first). So, it is a lot confusing to me. Any ideas?
Thanx.
Nov 16 '07 #3

P: 3
I forgot to say something very important. I do not know previously, the size of array (vector) or associative array (map). They grow interactively in execution time and their size may vary in a wide range ( to few hundreeds from hundreeds of thousands and even million).
Nov 16 '07 #4

weaknessforcats
Expert Mod 5K+
P: 9,197
What I need is exaclty what I try to represent.An array with each element having an associative array.
This would be a vector where each element is itself a vector.
Expand|Select|Wrap|Line Numbers
  1. vector< vector<Object*> > bank;
  2.  
Now the bank[i][j]syntax will work.

If you are accessing by a key then you may want a map where each element is itself another map.
Expand|Select|Wrap|Line Numbers
  1. map< int, map<int, Object*> > bank;
  2.  
Here you would specify a key to get a map and then specify another key to get an Object* from that second map.

Here you could use bank[i][j] to get on Object*.
Nov 16 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.