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

vector question

P: 3
hello.. just started learning cpp so my question might be bit stupid.. sorry 4 that.


Expand|Select|Wrap|Line Numbers
  1.  
  2. class Parser{
  3. public:
  4.     static vector<device*> parseConfiguration(ifstream& configFile) {
  5.         string line;
  6.         vector<device*> myDevices;
  7. ..
  8. ...
  9. ...
  10.  
  11.             if (device.compare("server") == 0) {
  12.                 int bufferSize;
  13.                 Id = helpFunctions::stoi(lineInVector[1]);
  14.                 name = lineInVector[2];
  15.                 bufferSize = helpFunctions::stoi(lineInVector[3]);
  16.                 server newServer(Id, name, bufferSize);
  17.                 newServer.setSerialId(myDevices.size());
  18.                 myDevices.push_back(&newServer);
  19.  
  20.                 cout << newServer.serialId << endl;
  21.                 cout << newServer.type << endl;
  22.             }
  23.  
  24.  
  25. ...
  26. ....
  27. ...
  28.  
  29.         }
  30.         return myDevices;
  31.     }
  32.  
  33.  
I have a functions creating a server with specific Id and putting it in vector of servers. Now, the type is defined in the constructor as 's' for server:
Expand|Select|Wrap|Line Numbers
  1. server::server(int Id, string name, int bufferSize)
  2. {
  3.  
  4.     this->type = 's';
  5.  
  6.     this->Id=Id;
  7.     this->name=name;
  8.     this->bufferSize=bufferSize;
  9.  
  10. }
  11.  
pretty much the same thing I have for my printers but I have 'p' in the type.

now - when I do the cout right after creating the server in the first code I get type 115 stands for 's'.

when checking it with debug/expression view with breakpoint on the return myDevices lets say myDevices[0]->type I get -1079945296
myDevices[1]->type I get 0
myDevices[2]->type I get 125713

Now Found another thing - I have 4 line in my file - 3 of them adding new servers and the 4 one is adding new printer. myDevices[3]->type ( or any other property like Id for that matter ) is returning me ( in the expression view ) Target request failed: Cannot access memory at address 0x0 As if it doesn't exists but I pushback printers also:

Expand|Select|Wrap|Line Numbers
  1. if (device == "printer") {
  2.                 int serverId;
  3.                 int paperAmount;
  4.                 string type;
  5.                 Id = helpFunctions::stoi(lineInVector[1]);
  6.                 name = lineInVector[2];
  7.                 serverId = helpFunctions::stoi(lineInVector[3]);
  8.                 paperAmount = helpFunctions::stoi(lineInVector[4]);
  9.                 type = lineInVector[5];
  10.                 //if(helpFunctions::IsServerAvailable(myDevices, serverId)){
  11.                     printer newPrinter(Id, name, serverId, paperAmount, true);
  12.                     if (type == "BLACK")
  13.                         newPrinter.setColor(false);
  14.                     newPrinter.setSerialId(myDevices.size());
  15.                     myDevices.push_back(&newPrinter);
  16.  
  17.                     cout << newPrinter.type << endl;
  18.                 //}
  19.             }
  20.  
  21.  
can someone please help me figure it out - been searching the net 4ever.

So It actually 2 questions since I'm not sure how to use vectors.
1. How come I get garbage right after push back.
2. Where have I lost my printer?
Feb 26 '09 #1
Share this Question
Share on Google+
2 Replies


Savage
Expert 100+
P: 1,764
@romand

Expand|Select|Wrap|Line Numbers
  1.             if (device.compare("server") == 0) {
  2.                 int bufferSize;
  3.                 Id = helpFunctions::stoi(lineInVector[1]);
  4.                 name = lineInVector[2];
  5.                 bufferSize = helpFunctions::stoi(lineInVector[3]);
  6.                 server newServer(Id, name, bufferSize);
  7.                 newServer.setSerialId(myDevices.size());
  8.                 myDevices.push_back(&newServer);
  9.  
  10.                 cout << newServer.serialId << endl;
  11.                 cout << newServer.type << endl;
  12.             }
Your new server variable is created inside the if block so it resides at different scope than the rest of the function code.It is created on stack,not on heap so when the if block is finished it gets automatically freed by the system.Create your server on the heap..

2°Probably the same as 1°.Have you checked the vector size?
Feb 26 '09 #2

P: 3
tnx.. finally after few hours a friend helped me changing some stuff and told me to use map instead of vectors so my program looks different now. tnx anyway.
Feb 26 '09 #3

Post your reply

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