Durgal
Durgal

Reputation: 25

Displaying an array of char from an object

I am making a simple program that allows me to set the name of an object by sending an array of characters into a public member function which then changes a private variable called name. I then tell my class to spit the name variable out so I can see if it work correctly -- but my output comes out mostly garbled.

ie, I input

"Apple"

and the output becomes

"AAAAA╠╠╠╠Apple"

I notice that as I change the word, the number of repeating characters mimics the number of characters in the word - but I cannot seem to figure out where I messed up with my program. Here is my code:

*Note: code below now works as expected.

    #include <iostream>
    #include <cstring>

    using namespace std;

    class Item{

         public:
    ~Item() { delete [] name; }

    char *setName(char * inputName)
    {
        name = new char[strlen(inputName)+1];

        int n = 0;
        for(n = 0; n<strlen(inputName); n++)
            name[n] = inputName[n];

        name[n] = '\0';

            return name;
    }

        private:
            char *name;
};


    int main()
    {
        char objectname[] = "Apple";

        Item Apple;
        cout << Apple.setName(objectname);

        int input;
        cin >> input; //pause program
    }

Upvotes: 1

Views: 520

Answers (1)

David G
David G

Reputation: 96835

As I said in my comment, you never allocate memory for name. When using name[n] you'll be dereference addresses that don't belong to you. This causes Undefined Behavior in your program, meaning anything can happen that doesn't conform to the logic of your program. It can even compile just fine, but you can't rely on that.

Anyway, you need to allocate memory for name. But for that you need to know the size of the memory to assign it to. We don't know the size of the memory (it can an arbitrary string that is sent to setName). Therefore name has to be a pointer:

char* name;

Then we can assign memory to it. BTW, char name[] is a zero-sized array, which is illegal in C++.

Inside setName, you need to get the size of the string that is sent to it. strlen is right for the job, but you also need to make room for the null-terminator ('\0'). It is the character that is appened to the end of every string, if you don't add it and try to print name later on, std::cout won't know where to stop printing each character (that's also Undefined Behavior).

Inside setName, this should be the assignment of the dynamic memory to name:

name = new char[strlen(inputName) + 1];

meaning: (number of characters in inputName + the null terminator). Note: strlen doesn't count the null-terminator.

Now, to set the null-terminator, you simply do this below the for() loop:

name[n] = '\0';

Moreover, when you are finished using the memory in name, you have to delete it. You do that using delete[]:

delete[] name;

The recommended alternative is to use std::string the standard C++ string class that manages memory by itself.

Upvotes: 2

Related Questions