User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 401,732 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,188 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Views: 1843 | Replies: 4
Reply
Join Date: Mar 2006
Posts: 4
Reputation: shlinky is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
shlinky shlinky is offline Offline
Newbie Poster

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

  #1  
Mar 23rd, 2006
Hi!

I have a class called anItem which is defined something like this :
class anItem
{
   CString string1;
   CString string2;
   long bla1;
   long bla2;
}

ok should be enough. Constructor/Destructor are empty.

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

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 :

class anItem
{
   CString string1;
   CString string2;
   char string3[10000]; //new char array
   long bla1;
   long bla2;
}

above is OK!

class anItem
{
   CString string1;
   CString string2;
   CString string3; // new CString
   long bla1;
   long bla2;
}

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!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Posts: 6,060
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 419
Super Moderator
Narue's Avatar
Narue Narue is online now Online
Expert Meanie

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

  #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 a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Mar 2006
Posts: 4
Reputation: shlinky is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
shlinky shlinky is offline Offline
Newbie Poster

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

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

class DataTreeItem  
{
public:
	DataTreeItem();
	virtual ~DataTreeItem();

CString projectpath;
	CString spec;
	CString name;
	char local[1024];
	//CString lspec;
	int size;
	int type;
	long version;
	long checkedout;
	HTREEITEM treeitem;
	IVSSItem * vssitem;
};


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  
Join Date: Sep 2004
Posts: 6,060
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 419
Super Moderator
Narue's Avatar
Narue Narue is online now Online
Expert Meanie

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

  #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 a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Nov 2005
Location: Canada
Posts: 235
Reputation: dwks will become famous soon enough dwks will become famous soon enough 
Rep Power: 4
Solved Threads: 20
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

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

  #5  
Mar 25th, 2006
So this doesn't run:
CString bad;
bad[999] = 'a';
but this does:
char good[10000];
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.
#include <iostream>
#include <string>

int main(void) {
    string name;

    std::cout << "Enter your name: ";
    std::cin >> name;

    std::cout << "Hello, " << name << '!' << std::endl;

    return 0;
}
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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 9:26 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC