save self-defined object in vector problem

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2005
Posts: 23
Reputation: alone2005 is an unknown quantity at this point 
Solved Threads: 0
alone2005's Avatar
alone2005 alone2005 is offline Offline
Newbie Poster

save self-defined object in vector problem

 
0
  #1
Jul 22nd, 2005
I define a class which hold some variables first:
  1. class Command{
  2. string cHandle;
  3. int numPara;
  4. vector<int> cPara;
  5. public:
  6. Command();
  7. Command(string ucData);
  8. //...............more functions here
  9. }; //end of class definition

Then I declare an object of Command and wanna store it in a vector
  1. vector<Command> cv;
  2. Command c;
  3. //... some operation of c, tested ok
  4. cv.push_back(c); //segmentation fault here

The thing is if I only initial c.cHandle, everything ok
if I initial both c.cHandle and c.cPara[ ]
I got a segmentation fault at the last line
what could be my mistake? thanks.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 23
Reputation: alone2005 is an unknown quantity at this point 
Solved Threads: 0
alone2005's Avatar
alone2005 alone2005 is offline Offline
Newbie Poster

Re: save self-defined object in vector problem

 
0
  #2
Jul 22nd, 2005
I kind figure out where is the problem, cause I wrote a copy constructor in Command class
if I comment this function out, everything in peace
  1. Command::Command(const Command& c){
  2. cHandle=c.getHandle();
  3. numPara=c.getNumPara();
  4. for(int i=0;i<numPara;i++) cPara[i]=c.getPara(i);
  5. }

But now I am even more confused, cause this function is only defined but has never been
used, how could it cause a memory fault? help pls?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,459
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 252
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: save self-defined object in vector problem

 
0
  #3
Jul 22nd, 2005
Originally Posted by alone2005
But now I am even more confused, cause this function is only defined but has never been used, how could it cause a memory fault? help pls?
As usual, the answer is probably in the code that you haven't show. Try paring the code down to a minimum that demonstrates the problem, but is yet fully compileable as-is. Then we can provide real answers. Otherwise we play 20 questions until you randomly post the problem spot. As a bonus, you may receive advice on how to better other unrelated portions of your code.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 23
Reputation: alone2005 is an unknown quantity at this point 
Solved Threads: 0
alone2005's Avatar
alone2005 alone2005 is offline Offline
Newbie Poster

Re: save self-defined object in vector problem

 
0
  #4
Jul 22nd, 2005
Originally Posted by Dave Sinkula
As usual, the answer is probably in the code that you haven't show. Try paring the code down to a minimum that demonstrates the problem, but is yet fully compileable as-is. Then we can provide real answers. Otherwise we play 20 questions until you randomly post the problem spot. As a bonus, you may receive advice on how to better other unrelated portions of your code.
Sorry i didn't post the full code only because it involve too much other classes, I will try to make a compilable short version here: 3 files, Command.h, Command.cpp, test.cpp
  1. #ifndef _USER_COMMAND_
  2. #define _USER_COMMAND_
  3.  
  4. #include <string>
  5. #include <vector>
  6. #include <iostream>
  7.  
  8. using namespace std;
  9.  
  10. class UserCommand{
  11. string cHandle; //definition of command
  12. int numPara;
  13. vector<int> cPara; //parameter of command
  14. public:
  15. UserCommand();
  16. UserCommand(string ucData);
  17. //copy constructor
  18. UserCommand(const UserCommand& uc);
  19.  
  20. void commandParser(string ucData);
  21. inline string getHandle() const {return cHandle;}
  22. inline int getPara(int i)const {return cPara[i];}
  23. inline int getNumPara() const {return numPara;}
  24. }; //end of class definition
  25. #endif

  1. #include "Command.h"
  2.  
  3. using namespace std;
  4.  
  5. UserCommand::UserCommand():cHandle(""), numPara(0) { }
  6.  
  7. UserCommand::UserCommand(string ucData):cHandle(""), numPara(0){
  8. commandParser(ucData);
  9. }
  10.  
  11. //Copy constructor
  12. UserCommand::UserCommand(const UserCommand& ucc){
  13. cHandle=ucc.cHandle;
  14. numPara=ucc.numPara;
  15. for(int i=0;i<ucc.numPara;i++) cPara[i] = ucc.cPara[i];
  16. }
  17.  
  18. void UserCommand::commandParser(string ucData){
  19. //in fact this function will extract para from string, now just make it simple
  20. cHandle="handle";
  21. numPara=5;
  22. for(int i=0; i<5; i++) cPara.push_back(i);
  23. }

  1.  
  2. #include "Command.h"
  3.  
  4. using namespace std;
  5.  
  6. int main(){
  7. UserCommand uc;
  8. vector<UserCommand> ucv;
  9.  
  10. string input=" ";
  11. uc.commandParser(input);
  12. ucv.push_back(uc); //segmentation fault here
  13.  
  14. return 0;
  15. }

After long debug I found that if I delete the copy constructor, it will work.
Or if I don't delelte cc, then only initialize <cHandle> in UserCommand::commandParser( ) method and leave <cPara> empty, it will work too. So I must have some missing
knowledge about copy constructor?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC