473,387 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Memory access error with STL composed types

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
4 2249
weaknessforcats
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
tuxman
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
tuxman
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
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;
  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

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

Similar topics

8
by: Benjamin Scott | last post by:
Hello. I attempted to build a compound dictionary: len(Lst)=1000 len(nuerLst)=250 len(nuestLst)=500 Dict={}
3
by: alanrn | last post by:
I would like to start a dialog on how to implement the equivalent functionality of UNIX shared memory in .NET. I work with a factory automation system. The bulk of the system is written in C/C++....
5
by: Ian | last post by:
I have and Access 97 DB that prints a report that has many pages, sometime about 800-1000 page, each page contains a colour picture along with text boxes. The pictures are displayed on the...
12
by: aling | last post by:
Have following code snip: struct struc { int member1; int member2; } ; printf("&((struc*)0)->member2=%p\n", &((struc*)0)->member2); In VC7.1, the output is 4, the offset of member2 in struc.
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
94
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring...
2
by: Mike | last post by:
Hi, I am new to C and having problems with the following program. Basically I am trying to read some files, loading data structures into memory for latter searching. I am trying to use structres...
29
by: K. Jennings | last post by:
I would like to get the result of malloc() such that it is aligned on a given boundary - 8, in this case. Assuming that sizeof(u64) is 8, will u64 *p = malloc(N) ; do the trick in general? ...
2
jlm699
by: jlm699 | last post by:
Greetings all, I'm using a PHP-based web page to access an SQL database containing logs which has grown quite large over time. A few weeks ago I recieved the following error: "Fatal error: Allowed...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.