-1

I am trying to initialize a char* and allocate memory for a specific amount of chars (9 in this case), but when I run the program and look at the value of char* (by using a breakpoint), I can see that a lot of nullptr characters have been added (as seen in the image). Where do these characters come from? and how do I avoid having them added to the char*? (I have to use char*)

char* Text = new char[9] {'0', '0', '0', '0', '0', '0', '0', '0', '0'};

What I get from this char* is: 000000000yyyyYYY3/4\x15a\x17Y\v

SCREENSHOT OF CODE

Thanks a lot.

Umeer
  • 1
  • 2
  • 3
    All questions here should have all relevant information ***in the question itself as plain text***. Links can stop working at any time making questions meaningless. Code, data, or errors shown as images cannot be copy/pasted; or edited or compiled for further research and investigation. Please [edit] this question, removing and replacing all links and images with all relevant information as plain text. All code must meet all requirements of a [mre]. You can find many other questions here that explain everything in plain text, please use them as an example for how your question should look. – Sam Varshavchik Jan 11 '21 at 04:06
  • 4
    The debugger assumes that a `char*` variable points to a nul-terminated string; but yours points to a sequence of characters that is not in fact nul-terminated, so the debugger proceeds to read and display whatever random garbage happens to be in memory beyond your array. This is not a problem in itself, it would only become a problem if you ever use `Text` in a way that expects it to be nul-terminated. – Igor Tandetnik Jan 11 '21 at 04:11
  • @IgorTandetnik That looks good as an answer, actually (if there isn't a duplicate already) – user202729 Jan 11 '21 at 04:15
  • If you're using visual Studio debugger, see [c++ - How to display a dynamically allocated array in the Visual Studio debugger? - Stack Overflow](https://stackoverflow.com/questions/75180/how-to-display-a-dynamically-allocated-array-in-the-visual-studio-debugger) . – user202729 Jan 11 '21 at 04:15
  • @IgorTandetnik Thank you for the answer and the explanation! I really appreciate it, because I had completely forgotten about nul-termination. – Umeer Jan 11 '21 at 04:18
  • Or [eclipse](https://stackoverflow.com/questions/1824685/eclipse-c-debugging-see-content-of-an-array), or [xcode](https://stackoverflow.com/questions/484707/viewing-a-dynamically-allocated-array-with-the-xcode-debugger) (seriously, if you have an issue with the debugger, should specify which debugger you're using -- although it's possible to null-terminate the char array, it doesn't make sense if you don't spend to use it as a string) – user202729 Jan 11 '21 at 04:26

1 Answers1

2

Initializing char* and allocating memory for a specific amount adds nullptr characters

There is no such thing as a "nullptr character". You may be referring to the null terminator character, which is not a pointer and therefore not "nullptr".

'0'

This is not the null terminator literal. This is character literal for the numeral digit 0. The literal for the null terminator is '\0'.

What I get from this char* ...

This is presumably because the array is not null terminated and consequently the behaviour of the program would be undefined if you pass it to a character stream which would require a string to be null terimnated.

eerorika
  • 223,800
  • 12
  • 181
  • 301