1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

C++ - strcpy into array

Discussion in 'Mac Programming' started by Coolnat2004, Apr 12, 2009.

  1. macrumors 6502


    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";
    		counts[numwords] = 1;
    	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? :)
  2. macrumors 6502a


    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
  3. macrumors 6502


    Thank you for your help! I solved the problem by doing this before copying the word:
    list[numwords] = new char[strlen(word)];
  4. 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.
  5. macrumors 68040


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

  6. macrumors G5


    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?
  7. macrumors G3

    You're programming in C++, not C. Use the STL.
  8. macrumors 68040


    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.

  9. macrumors 6502


    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.
  10. macrumors 6502

    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.

Share This Page