Weird (?) problem using std::list ...

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

Join Date: Mar 2006
Posts: 4
Reputation: shlinky is an unknown quantity at this point 
Solved Threads: 0
shlinky shlinky is offline Offline
Newbie Poster

Weird (?) problem using std::list ...

 
0
  #1
Mar 23rd, 2006
Hi!

I have a class called anItem which is defined something like this :
  1. class anItem
  2. {
  3. CString string1;
  4. CString string2;
  5. long bla1;
  6. long bla2;
  7. }

ok should be enough. Constructor/Destructor are empty.

At another place I use this class as a template for a std::list.

  1. std::list<anItem> list;

I can add and get items from that list without any problem, UNLESS I do the following.

If I add another Item to the anItem-class, it only works if it is not a CString type.

Examples :

  1. class anItem
  2. {
  3. CString string1;
  4. CString string2;
  5. char string3[10000]; //new char array
  6. long bla1;
  7. long bla2;
  8. }

above is OK!

  1. class anItem
  2. {
  3. CString string1;
  4. CString string2;
  5. CString string3; // new CString
  6. long bla1;
  7. long bla2;
  8. }

Runtime Error ("Memory could not be read.").

Does any of you have an idea what the problem here is?
I am using MS Visual C++ 6.0; this is used in an MFC application.

Thanks!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 717
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Weird (?) problem using std::list ...

 
0
  #2
Mar 23rd, 2006
>I have a class called anItem which is defined something like this
I'll assume it's "something" like that rather than exactly like that because that class won't compile (no trailing semicolon) and even if it did would be unusable because all of the data members are private with no way to access them.

>Runtime Error ("Memory could not be read.").
Step through your code, and find out exactly when this happens and what you're doing at the time. Debugging remotely is difficult enough when the problem isn't vague.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 4
Reputation: shlinky is an unknown quantity at this point 
Solved Threads: 0
shlinky shlinky is offline Offline
Newbie Poster

Re: Weird (?) problem using std::list ...

 
0
  #3
Mar 23rd, 2006
youre right, sorry. here is the actual class :

  1. class DataTreeItem
  2. {
  3. public:
  4. DataTreeItem();
  5. virtual ~DataTreeItem();
  6.  
  7. CString projectpath;
  8. CString spec;
  9. CString name;
  10. char local[1024];
  11. //CString lspec;
  12. int size;
  13. int type;
  14. long version;
  15. long checkedout;
  16. HTREEITEM treeitem;
  17. IVSSItem * vssitem;
  18. };


so what happens is that the app crashes as soon as i compile with the comment removed and CString lspec is an attribute of the class.

Any Ideas anyone??
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 717
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Weird (?) problem using std::list ...

 
0
  #4
Mar 24th, 2006
>so what happens is that the app crashes
Define 'crash'.

>Any Ideas anyone??
No, because you're still not giving enough information. At this point, post a small program that exhibits the problem so that we can debug it personally rather than play twenty questions with you.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 251
Reputation: dwks has a spectacular aura about dwks has a spectacular aura about 
Solved Threads: 25
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: Weird (?) problem using std::list ...

 
0
  #5
Mar 25th, 2006
So this doesn't run:
  1. CString bad;
  2. bad[999] = 'a';
but this does:
  1. char good[10000];
  2. good[999] = 'a';

It looks like a CString is equivalent to a char*. In that case, you need to allocate memory dynamically for it or use a char array.

Your best bet would be to use a string, from <string>. They automatically resize themselves.
  1. #include <iostream>
  2. #include <string>
  3.  
  4. int main(void) {
  5. string name;
  6.  
  7. std::cout << "Enter your name: ";
  8. std::cin >> name;
  9.  
  10. std::cout << "Hello, " << name << '!' << std::endl;
  11.  
  12. return 0;
  13. }
dwk

Seek and ye shall find.

"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.

"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison

"The only real mistake is the one from which we learn nothing."
-- John Powell
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC