PDA

View Full Version : C++ - strcpy into array




Coolnat2004
Apr 12, 2009, 05:53 PM
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:

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
Apr 12, 2009, 06:39 PM
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.



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
Apr 12, 2009, 10:50 PM
Thank you for your help! I solved the problem by doing this before copying the word:
list[numwords] = new char[strlen(word)];

Kurukuru
Apr 13, 2009, 04:00 AM
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
Apr 13, 2009, 08:30 AM
Thank you for your help! I solved the problem by doing this before copying the word:
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
Apr 13, 2009, 10:19 AM
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?

cube
Apr 13, 2009, 10:27 AM
You're programming in C++, not C. Use the STL.

lee1210
Apr 13, 2009, 10:53 AM
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
Apr 13, 2009, 12:00 PM
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
Apr 13, 2009, 02:04 PM
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.