470,848 Members | 1,053 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,848 developers. It's quick & easy.

Memory access error with STL composed types

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
4 1981
9,208 Expert Mod 8TB
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
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?
Nov 16 '07 #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
9,208 Expert Mod 8TB
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;
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;
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.

Similar topics

8 posts views Thread by Benjamin Scott | last post: by
3 posts views Thread by alanrn | last post: by
5 posts views Thread by Ian | last post: by
12 posts views Thread by aling | last post: by
2 posts views Thread by Mike | last post: by
29 posts views Thread by K. Jennings | last post: by
2 posts views Thread by jlm699 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.