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:

vector < map < int , char * > > bank;
      bank . reserve ( 10 );
      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

bank [ 0 ] [ 0 ] = "b"; //Same error

Finally, the following inserted in place of the line above, works.
Code: ( text )

map < int , char * > data;
      data [ 0 ] = "a";
      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
Code: ( text )

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.

Recommended Answers

All 3 Replies

vector < map < int , char * > > bank;
      bank . reserve ( 10 );
      if ( bank [ 0 ] [ 0 ] == "a" ) //Memory access error

this code has *two* problems.
one: bank[0] is invalid; the vector is empty. there is no bank[0].
two: the code may not work even if bank[0] refers to a valid element of type
map < int , char * > map<>::operator[] would give a char*&.
operator == would give you a comparison between the two pointers. (what you need is strcmp).

it would be best to change the vector < map < int , char * > > to a
vector < map < int , string> > ; the == will then test equivalence. it could also avoid errors with the pointer being invalid or the c style string pointed to being overwritten.

> 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
> bank [ i ] [ j ] = "some" ;

perhaps you could write a couple of functions

// bank[i][j] = str ;
void insert( map < int , char * >& bank, size_t i, 
                          int j, const char* str )
{
  if( bank.size() < (i+1) ) bank.resize(i+1) ;
  bank[i][j] = str ; // strdup(str) ?
}
// bank[i][j] == str 
bool equals( const map < int , char * >& bank, size_t i,
                           int j, const char* str )
{
  return( bank.size() > i ) &&
      ( bank[i].find(j) !=  bank[i].end() ) &&
      ( bank[i][j] != 0 ) && 
      ( strcmp( bank[i][j], str ) == 0 ) ;
}

Thanx vijayan121. You're right. I was exauted and don't see the NULL address access (bank [0]). I will do as you suggest. But one thing keeps me scratching my head:
Why the following work:

vector < map < int , char * > > bank;
map < int , char * > data;
      data [ 0 ] = "a";
      bank . push_back ( data );

and this doesn't:

vector < map < int , char * > > bank;
map < int , char * > data;
      data [ 0 ] = "a";
      bank [0] = data;

????

I've seen a lot of examples of STL vectors that was constructed inserting elements only with subscript operator.

for (i=0;i<10;i++) vec [i]=i;

It's doesn't make sense to me.

> Why the following work:
bank . push_back ( data ); adds a new element to the sequence,

> and this doesn't:
bank [0] = data; tries to access and then assigns to a non-existing element.

> I've seen a lot of examples of STL vectors that was constructed inserting elements only with subscript operator.

vector<int> vec(10) ;
for ( int i=0;i<10;i++) vec [i]=i;

would work; there are 10 elements in the vector

vector<int> vec ;
for ( int i=0;i<10;i++) vec [i]=i;

would not; the vector is empty

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.