473,387 Members | 1,455 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.

Retrieving a class instance item from a vector

Having problem with retrieving a class instance item from a Vector.
This is the result of the code below.

Also a weird note: If I dont declare as:
TYPE_VECTOR_BANKED_MEMORY_DATA
bankedDataMemoryLayout(NUMBER_BANKS);
but an empty vector
TYPE_VECTOR_BANKED_MEMORY_DATA bankedDataMemoryLayout;

The program crashes on the 3rd insertion???

Additionally in the printout routine I am having a problem with using
an iterator. Can I not use an iterator?
PRIMING THE VECTOR:

STARTING ADDRESS: [c000] ENDING ADDRESS: [feff]
STARTING ADDRESS: [380000] ENDING ADDRESS: [3cbfff]
STARTING ADDRESS: [3d0000] ENDING ADDRESS: [3dbfff]
STARTING ADDRESS: [3e0000] ENDING ADDRESS: [3ebfff]
The size is: 8

Dumping contents of banks

STARTING ADDRESS: [0] ENDING ADDRESS: [0]
STARTING ADDRESS: [fdfdfdfd] ENDING ADDRESS: [dddddddd]
STARTING ADDRESS: [dddddddd] ENDING ADDRESS: [dddddddd]
STARTING ADDRESS: [dddddddd] ENDING ADDRESS: [dddddddd]
STARTING ADDRESS: [dddddddd] ENDING ADDRESS: [dddddddd]
STARTING ADDRESS: [dddddddd] ENDING ADDRESS: [dddddddd]
STARTING ADDRESS: [dddddddd] ENDING ADDRESS: [dddddddd]
STARTING ADDRESS: [dddddddd] ENDING ADDRESS: [dddddddd]
THE CODE PROGRAM:
---------------------------------------

class bankedMemoryDataClass
{
public:
//bankedMemoryDataClass(){}
bankedMemoryDataClass():
bankStartingMemoryAddress(0),
bankEndingMemoryAddress(0){};
~bankedMemoryDataClass(){}

unsigned long int getBankStartingMemoryAddress() const { return
bankStartingMemoryAddress;}
unsigned long int getBankEndingMemoryAddress() const {return
bankEndingMemoryAddress;}
void setBankStartingMemoryAddress(unsigned long int start){
bankStartingMemoryAddress = start;}
void setBankEndingMemoryAddress(unsigned long int end){
bankEndingMemoryAddress = end;}

private:

unsigned long int bankStartingMemoryAddress;
unsigned long int bankEndingMemoryAddress;

};

const int NUMBER_BANKS = 4;

typedef vector<bankedMemoryDataClass> TYPE_VECTOR_BANKED_MEMORY_DATA;

TYPE_VECTOR_BANKED_MEMORY_DATA bankedDataMemoryLayout(NUMBER_BANKS);
TYPE_VECTOR_BANKED_MEMORY_DATA::iterator
iteratorBankedDataMemoryLayout;
TYPE_VECTOR_BANKED_MEMORY_DATA::reference
referenceToBankData(TYPE_VECTOR_BANKED_MEMORY_DATA &);
//---------------------------------------------------------------------------

void initializeBankedMemoryLayout(void){

bankedMemoryDataClass tempObject;

iteratorBankedDataMemoryLayout = bankedDataMemoryLayout.begin();
cout.setf(std::ios::showbase);
tempObject.setBankStartingMemoryAddress(0xC000);
tempObject.setBankEndingMemoryAddress( 0xFEFF);
cout << "STARTING ADDRESS: [" << hex <<
tempObject.getBankStartingMemoryAddress() << "] ";
cout << "ENDING ADDRESS: [" << hex <<
tempObject.getBankEndingMemoryAddress() << "]" << endl;

bankedDataMemoryLayout.insert(iteratorBankedDataMe moryLayout,tempObject);

iteratorBankedDataMemoryLayout++;
tempObject.setBankStartingMemoryAddress(0x380000);
tempObject.setBankEndingMemoryAddress( 0x3CBFFF);
cout << "STARTING ADDRESS: [" <<
tempObject.getBankStartingMemoryAddress() << "] ";
cout << "ENDING ADDRESS: [" <<
tempObject.getBankEndingMemoryAddress() << "]" << endl;

bankedDataMemoryLayout.insert(iteratorBankedDataMe moryLayout,tempObject);

iteratorBankedDataMemoryLayout++;
tempObject.setBankStartingMemoryAddress(0x3D0000);
tempObject.setBankEndingMemoryAddress( 0x3DBFFF);
cout << "STARTING ADDRESS: [" <<
tempObject.getBankStartingMemoryAddress() << "] ";
cout << "ENDING ADDRESS: [" <<
tempObject.getBankEndingMemoryAddress() << "]" << endl;

bankedDataMemoryLayout.insert(iteratorBankedDataMe moryLayout,tempObject);

iteratorBankedDataMemoryLayout++;
tempObject.setBankStartingMemoryAddress(0x3E0000);
tempObject.setBankEndingMemoryAddress( 0x3EBFFF);
cout << "STARTING ADDRESS: [" <<
tempObject.getBankStartingMemoryAddress() << "] ";
cout << "ENDING ADDRESS: [" <<
tempObject.getBankEndingMemoryAddress() << "]" << endl;

bankedDataMemoryLayout.insert(iteratorBankedDataMe moryLayout,tempObject);
//cout.setf(std::ios::noshowbase);

cout << "The size is: " << bankedDataMemoryLayout.size() << endl;
}

void printOutBankedMemoryLayout(void){

bankedMemoryDataClass tempObject;

cout << endl << "Dumping contents of banks" << endl << endl;

for (int i=0; i<8; i++)
{
tempObject = bankedDataMemoryLayout[i *
sizeof(bankedMemoryDataClass)];

//cout << "bank (" << *iteratorBankedDataMemoryLayout << ") ";
cout << "STARTING ADDRESS: [" <<
tempObject.getBankStartingMemoryAddress() << "] ";
cout << "ENDING ADDRESS: [" <<
tempObject.getBankEndingMemoryAddress() << "]" << endl;
}
/*
for (iteratorBankedDataMemoryLayout = bankedDataMemoryLayout.begin();
iteratorBankedDataMemoryLayout != bankedDataMemoryLayout.end();
iteratorBankedDataMemoryLayout++){

tempObject =
bankedDataMemoryLayout.at((int)*iteratorBankedData MemoryLayout);

cout << "bank (" << *iteratorBankedDataMemoryLayout << ") ";
cout << "STARTING ADDRESS: [" <<
tempObject.getBankStartingMemoryAddress << "] ";
cout << "ENDING ADDRESS: [" <<
tempObject.getBankEndingMemoryAddress << "]" << endl;
}
*/
}

int main(int argc, char* argv[])
{
int index;

initializeBankedMemoryLayout();
printOutBankedMemoryLayout();

return 1;
}

Jul 28 '05 #1
4 1684
co**********@yahoo.com wrote:
Having problem with retrieving a class instance item from a Vector.
This is the result of the code below.
[...]
void initializeBankedMemoryLayout(void){

bankedMemoryDataClass tempObject;

iteratorBankedDataMemoryLayout = bankedDataMemoryLayout.begin();
....and you're hanging onto this iterator...
cout.setf(std::ios::showbase);
tempObject.setBankStartingMemoryAddress(0xC000);
tempObject.setBankEndingMemoryAddress( 0xFEFF);
cout << "STARTING ADDRESS: [" << hex <<
tempObject.getBankStartingMemoryAddress() << "] ";
cout << "ENDING ADDRESS: [" << hex <<
tempObject.getBankEndingMemoryAddress() << "]" << endl;

bankedDataMemoryLayout.insert(iteratorBankedDataMe moryLayout,tempObject);
.... which is invalidated by this 'insert'...

iteratorBankedDataMemoryLayout++;
.... incrementing it here causes _undefined behaviour_ ...

bankedDataMemoryLayout.insert(iteratorBankedDataMe moryLayout,tempObject);
.... and trying to use it here again causes _undefined behaviour_ as well.
[...]


Do NOT use 'insert'. Use 'push_back()' instead.

V
Jul 28 '05 #2
Ok, I am using the push_back now for insertion, but retrieving actually
fails / aborts on retrieving the first object (in which the data is
incorrect.).

void initializeBankedMemoryLayout(void){

bankedMemoryDataClass tempObject;

tempObject.setBankStartingMemoryAddress(0xC000);
tempObject.setBankEndingMemoryAddress( 0xFEFF);
cout << "STARTING ADDRESS: [" <<
tempObject.getBankStartingMemoryAddress() << "] ";
cout << "ENDING ADDRESS: [" <<
tempObject.getBankEndingMemoryAddress() << "]" << endl;
bankedDataMemoryLayout.push_back(tempObject);
}
void printOutBankedMemoryLayout(void){

bankedMemoryDataClass tempObject;
cout << endl << "Dumping contents of banks" << endl << endl;

for (int i=0; i<8; i++)
{
tempObject = (bankedMemoryDataClass)bankedDataMemoryLayout.at(i *
sizeof(bankedMemoryDataClass));

cout << "STARTING ADDRESS: [" <<
tempObject.getBankStartingMemoryAddress() << "] ";
cout << "ENDING ADDRESS: [" <<
tempObject.getBankEndingMemoryAddress() << "]" << endl;
}

Jul 29 '05 #3
co**********@yahoo.com wrote:
Ok, I am using the push_back now for insertion, but retrieving actually
fails / aborts on retrieving the first object (in which the data is
incorrect.).

[...]
for (int i=0; i<8; i++)
{
tempObject = (bankedMemoryDataClass)bankedDataMemoryLayout.at(i *
sizeof(bankedMemoryDataClass));
Why the hell do you use 'sizeof' here? RTFM about the meaning of the
argument to the 'at' member function.

And why do you need the 'at'? Why not simply use the indexing again?

cout << "STARTING ADDRESS: [" <<
tempObject.getBankStartingMemoryAddress() << "] ";
cout << "ENDING ADDRESS: [" <<
tempObject.getBankEndingMemoryAddress() << "]" << endl;
}


V
Jul 29 '05 #4
Resolved.

The problem is in the definition ot the vector.at(size_type n).
I thought to retrieve one needed:

for (int i=0; i<8; i++)
bankedDataMemoryLayout.at(i *sizeof(bankedMemoryDataClass));

but it is just
bankedDataMemoryLayout.at(i);

Thanks V.

Jul 29 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Sean W. Quinn | last post by:
Hey folks, I have a question regarding file handling, and the preservation of class structure. I have a class (and I will post snippets of code later in the post) with both primitive data...
5
by: franklini | last post by:
i cant seem to figure out what is wrong with this class. it has to do with the input/output stream override. please can somebody help me. #include <iostream> #include <string> #include <vector>...
2
by: franklini | last post by:
hello people i. can anybody help me, i dont know what is wrong with this class. it has something to do with the me trying to override the input output stream. if i dont override it, it works fine....
9
by: Daz | last post by:
Hello people! (This post is best viewed using a monospace font). I need to create a class, which holds 4 elements: std::string ItemName int Calories int Weight int Density
2
by: pvl_google | last post by:
Hi, I'm trying to extend an STL class with additional iterator functionality. In the simplified example below I added an extra iterator class with a dereferencing operator. This operator...
16
by: arnaudk | last post by:
I'm trying to design a simple container class for some data of different types based on a vector of structs, but the vector and struct are protected so that the implemenation of my container class...
0
bmallett
by: bmallett | last post by:
First off, i would like to thank everyone for any and all help with this. That being said, I am having a problem retrieving/posting my dynamic form data. I have a form that has multiple options...
0
by: tech | last post by:
Hi, I have developed a little class to help implement dynamic menus in our application which has a UI. This allows our engine to send a MenuItem object to the UI which is an observer of the...
2
by: =?ISO-8859-1?Q?Andr=E9_Luiz_Carvalho?= | last post by:
Hi there, I'm porting an application from Java to C++ / BREW so I can't use the stl, therefore, I have to implement the basic structures that the app use in Java. I'm implementing a Hashtable...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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.