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.


10 Years
Discussion Span
Last Post by dwks

>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.


youre right, sorry. here is the actual class :

class 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??


>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.


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;
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.