user3710529
user3710529

Reputation: 5

Having trouble with pointers

I have some trouble with displaying data from a couple of pointer chars.

int main()
{
    char *arr = new char();
    for (int t = 0;t < 10;t++)
    {
        int size = Generator::GetInstance()->Generate(arr);
        for (int generateChars = 0;generateChars < size;generateChars++)
        {
            std::cout << &arr[generateChars] << " ";
        }
        std::cout << std::endl;
    }
    return 0;
}

and the Generator class

int Generator::Generate(char *ar)
{
    srand(time(NULL));
    int size = (rand() % 10)+1;
    arr = new char[size];
    for (int t = 0;t < size;t++)
    {
        int path = rand() % 4;
        switch (path)
        {
            case 0:
                arr[t] = 'U';
                break;
            case 1:
                arr[t] = 'R';
                break;
            case 2:
                arr[t] = 'D';
                break;
            case 3:
                arr[t] = 'L';
                break;
        }
    }
    arr[size] = '\0';
    *ar = *arr;
    return size;
}

What am I doing wrong this time and why is it displaying junk data each time I am trying something. Sorry if this is something that is answered before already.

Upvotes: 0

Views: 72

Answers (2)

Fearnbuster
Fearnbuster

Reputation: 896

Change your "Generate" functions parameter from a pointer to a pointer reference:

int Generator::Generate(char *& ar)

And change the line:

*ar = *arr;

that is in your "Generate" function to:

ar = arr;

Also remember to clear any dynamic memory that "ar" may point to, when "Generate" is called.

I tested this code and it worked for me...

EDIT In the "for" loop that displays your array, change the line:

std::cout << &arr[generateChars] << " ";

to:

std::cout << arr[generateChars] << " ";

Upvotes: 1

comingstorm
comingstorm

Reputation: 26117

If you try to print a char *, C++ assumes that it is a C-style null-terminated string.

It appears that you are allocating (and passing a pointer into) a char array without a terminating null -- so, the I/O library will follow that character with all the junk data that happens to be in memory, until it happens to encounter a null byte.

A simple solution is to pass either a char or a char &, instead of a char *, so that the I/O will correctly interpret it as a single character.

However, that will not fix your memory leaks, which are a separate problem. If you want to use C++ for anything beyond toy programs, you should make a habit of delete-ing anything allocated by new when you no longer need it.

Finally, as mentioned in comments, your code is very non-idiomatic for C++. It is actually terribly educational to play around with low-level code like this, but the standard library has std::string's and all sorts of containers that make it easier to manage your memory correctly, so make sure you learn how to use them, as well as understanding the lower-level features they are built on...

Upvotes: 3

Related Questions