Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Coolnat2004

macrumors 6502
Original poster
Jan 12, 2005
479
4
So, I'm pretty new to C++. I have a problem copying a c-style string into an empty array of c-style strings. Here is the relevant code:

Code:
bool addToList(char ** list, int * counts, char * word, int numwords) {
	bool found = false;
	(...)
	if (!found) {
		cout << "NOT";
		strcpy(list[numwords],word);
		counts[numwords] = 1;
		numwords++;
	}
	return true;
}

int main() {
	char **list;
	int *counts;
	counts = new int[100];
	list = new char*[100];
	int numwords = 0; // number of words
	(...)
	char * word;
	word = "asdf";
	addToList(list, counts, word, numwords);
	return 0;
}

The code compiles just fine, but it stops running when it hits the line with strcpy(). (If I comment that line out, it runs to the end)

I would try another method, but this is how the professor wants it to be done. Actually, he was using strcpy_s(), which is specific to Microsoft as I understand. I'm using Xcode, so that's not an option.

Are there any masters of C++ out there that can explain my error for me? :)
 

lazydog

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
Hi

I think your problem is that you've allocated space for pointers to your 100 strings, but you haven't actually allocated memory for the strings.

Code:
list = new char*[100];

So in the above, you've created an array of uninitialised pointers to strings. What you need to do in addToList() is allocate memory for the string before copying it.

Also, in addToList() you pass numwords by value when I imagine it would be more useful to pass it by reference (or a pointer) seeing that you increment it.

b e n
 

Coolnat2004

macrumors 6502
Original poster
Jan 12, 2005
479
4
Thank you for your help! I solved the problem by doing this before copying the word:
Code:
list[numwords] = new char[strlen(word)];
 
K

Kurukuru

Guest
Although not the same as strcpy_s, strncpy can be used to provide you with some protection of the destination buffer, by allowing you to limit the number of characters written to it.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Thank you for your help! I solved the problem by doing this before copying the word:
Code:
list[numwords] = new char[strlen(word)];

That thing is not big enough. strlen tells you the number of bytes starting at the address given until a null is reached, non-inclusive. This means it will tell you how many characters are in a C-style string, but not how many bytes are needed to hold those characters plus the null terminator. You should add one to the result of strlen.

Also, do you know the pattern you need to follow to properly delete all of this memory you're allocating? I.e. what should be deleted first, etc.?

-Lee
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
So, I'm pretty new to C++. I have a problem copying a c-style string into an empty array of c-style strings. Here is the relevant code:

Before you write any code, you should write down what data structures you will have. Because the data structures are the essential thing here; the code is just a small detail. The thread makes it look as if you are hacking your solution together without ever stepping back and thinking about what data you need to store. You can do that and succeed with a 50 line program. You will run into trouble at 500 lines, have no chance at 5,000 lines, and be utterly stumped at real world software.

So please: What will be your data structures?
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
I don't want to discount what gnasher729 and cube are saying here, but wanted to point out that the OP mentioned that this was for a class. The teacher/professor may have prescribed that C-style "strings" be used in a C-style array, rather than using std::vector and std::string, etc.

-Lee
 

Coolnat2004

macrumors 6502
Original poster
Jan 12, 2005
479
4
I did end up using strncpy() due to similar advice I found elsewhere (and in the book, imagine that!). I would rather use the STL, but the professor is teaching it this way (hopefully just for now).

I did add 1 to the length when copying it, because I realized that it was missing the \0. It is working beautifully. Thanks for the help and advice.
 

Sander

macrumors 6502a
Apr 24, 2008
521
67
I see that your professor explicitly required this, but I would certainly ask why. If this is only to show you a few classes from now that this tedious and error-prone stuff is done differently in C++, I'm all for it.

If it turns out that he stopped learning C++ himself after memorizing that "malloc is now spelled new and free is now delete" then you should seriously consider finding a different educator.

I'm not kidding.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.