The following code works fine with "Release" config, but gets this odd "HEAP_CORRUPTION_DETECTED" error when I try to execute in the "Debug" config. I am using visual studio 2005, default settings.

Also, I looked into deleting multi-dimensional objects, and it appears I am doing it correctly here (http://www.daniweb.com/forums/thread6511.html).

Pretty simple code, imho:

void readStrVec() {
	vector<string> strVec;
	string thisString = "";

	while(cin >> thisString) {
		strVec.push_back(thisString);
	}
	char **newCStrArray = new char*[strVec.size()];
	for(size_t i = 0; i < strVec.size(); i++) {
		newCStrArray[i] = new char[strVec[i].size()];
		strcpy(newCStrArray[i], strVec[i].c_str());
		//Alternatively: (same problem)
		//char *cstr = new char[strVec[i].size()];
		//strcpy(cstr, strVec[i].c_str());
		//newCStrArray[i] = cstr;
		cout << newCStrArray[i] << endl;
	}

	for(size_t i = 0; i < strVec.size(); i++) {
		delete [] newCStrArray[i];
	}
	delete [] newCStrArray;
}

Any ideas?

Thanks.

Recommended Answers

All 2 Replies

line 10 doesn't allocate room for the null terminator. Add 1 to the size() value.

commented: Good catch +17

*smacks forehead*

Thanks so much. Migrating from C (char *) to C++ strings had me carelessly forgetting about those pesky null characters, and the debug error didn't help much :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.