0

Why exactly is the char array I created and initialized with a value not being printed correctly and instead some random characters/garbage value being printed out?

Here is my code:

class CharArrayChecker
{
public:
    CharArrayChecker() {};
    char* GetCharArray();
};

char* CharArrayChecker::GetCharArray()
{
    char Output[32] = "This is a test";
    return Output;
}

int main()
{
    CharArrayChecker* CharArrayCheck = new CharArrayChecker();
    
    const char* Test = CharArrayCheck->GetCharArray();
    printf("Output: %s\n\n", Test);
    return 0;
}
JaMiT
  • 14,422
  • 4
  • 15
  • 31
AaySquare
  • 123
  • 10
  • 1
    `Output` is local to the method, its lifetime ends when the method returns – 463035818_is_not_an_ai Aug 07 '21 at 16:52
  • 2
    why are you not using `std::string`? `std::string` can be returned by value – 463035818_is_not_an_ai Aug 07 '21 at 16:52
  • `CharArrayChecker* CharArrayCheck = new CharArrayChecker();` -- C++ is not Java. There is no need to use `new` to create an object here. This does the same thing, with no chance of a memory leak: `CharArrayChecker CharArrayCheck{};`. In addition, C++ uses value-semantics, not reference semantics. If you are coming from Java, please unlearn what you know about objects and Java, as C++ does things differently. – PaulMcKenzie Aug 07 '21 at 17:18
  • @PaulMcKenzie Oh I am aware of that. I did not mean to keep the code line of creating object using new. As I am practicing using C++, I wanted to see how it works. Of course, I forgot if I am using new, I need to call delete too. – AaySquare Aug 07 '21 at 18:45
  • @463035818_is_not_a_number Is that because `Output` is being allocated on Stack instead of Heap, right? So changing that to: `char* Output = new char[32]("This is a test");` would be better? As for why I am not using `std::string` is because I wanted to learn about how char pointers and arrays work. – AaySquare Aug 07 '21 at 18:50
  • @AaySquare `So changing that to: char* Output = new char[32]("This is a test"); would be better?` -- Who is responsible for cleaning up that memory? If you mean by "better" that the data will last outside the function, then yes, that is what will happen. But no C++ programmer would need to do this, as again, you're creating a maintenance headache (potential memory leaks) for no reason. Just use `std::string` -- pointers are no big deal -- you declare a pointer, you point it somewhere valid. There is nothing to learn there *except* when you get into pointer Hades and try to untangle a mess. – PaulMcKenzie Aug 07 '21 at 19:14
  • And if you really want to learn about pointers, create your own string class. You learn basically nothing doing things this way -- at least when you create a class, not only do you learn about pointers, but proper encapsulation, how to implement correct copy semantics, move operations, etc. The one-off pointer stuff -- you don't learn anything from that. – PaulMcKenzie Aug 07 '21 at 19:18
  • @PaulMcKenzie So considering if I don't want to use `std::string` for now, to avoid "maintenance headache", can I also use smart pointer to declare `Output` instead? Or what about using `static` keyword like so: `static char Output[32] = "This is a test";` This also printed out the string fine. – AaySquare Aug 07 '21 at 19:30
  • making the char array static would compile and work, but its the wrong solution. Use dynamic allocation if you want the array outlive its scope, but then you should not use a raw pointer. A smart pointer would work, because it can manage the lifetime a dynamically allocated char array, but a `std::string` does that as well, and offers much more functionality on top. If you want to learn about pointers, perhaps the most important thing to learn is to never use a raw pointer to own an object. – 463035818_is_not_an_ai Aug 07 '21 at 21:52
  • @463035818_is_not_a_number Why is it wrong solution to use static? Also, how would you declare my char array as a smart pointer? – AaySquare Aug 07 '21 at 22:38
  • So I just created a smart pointer like so: `std::unique_ptr Output(new char[32]("This is a test"));`. Then I do: `return Output.get();`. However, this is still giving garbage value. Am I incorrectly using smart pointer or what is wrong? – AaySquare Aug 07 '21 at 23:10
  • it is the wrong solution because you want to return a string not a pointer to a char array that is a local static in the function. Don't use a hammer for a screw. The right way to return a string is to return a `std::string` – 463035818_is_not_an_ai Aug 07 '21 at 23:16
  • you cannot return a character array from a function. It is as simple as that. It is not possible. You can return a character array wrapped in a class. Someone already wrote that wrapper, it is `std::string` ;) – 463035818_is_not_an_ai Aug 07 '21 at 23:17
  • fwiw, the accepted answer is the duplicate is correct, but it is not idiomatic c++. And it is not returning an array. It is returning a pointer to an array – 463035818_is_not_an_ai Aug 07 '21 at 23:19

0 Answers0