what is with char arrays?

OK, I never use char/arrays. I don't want to play with naked pointers/char/arrays when we have industrial strength tools to take care of these things in most cases. However, I find with file IO, it's sort of hard to avoid having to mess around with char, and because I don't use them, they are a bit of unfamiliar to me.

Anyway, my question is what the heck is going on when you request some new space for a char pointer??? Like

char *chrBuffer;
chrBuffer = new char[5];

When I break and look at chrBuffer after using new, it shows chrBuffer is pointing to an array of 24 char. In Visual Studio, there is a text visualizer that shows you what is in there, and it shows 24 garbage characters. I don't care about the garbage, since we haven't initalized, but why is the array size 24 when the array was set to size 5????

If I assign chrBuffer to a string -

std::string stringObj = chrbuffer;

stringObj shows 24 characters

Thanks for any info.
That's because you've placed nothing into the memory chrBuffer points at; it contains whatever data was there before. C-style strings are terminated by a null character, so both the VS debugger and std::string find the end of the string by reading until they reach a null character, which probably happens to be 24 characters past the start of the array.
So initialize chrBuffer[4] to '\0' e.g.:
chrBuffer = new char [5]{};
Last edited on
OK Thanks. I expect garbage at that point, I just don't expect it to dangle on beyond the exact amount of memory requested. If you ask for 5 chars, it seems really weird to give you 24, null char or not.
It just points to the start of a location in memory where 5 characters have been allocated. Memory is contiguous, so there is likely more memory addressed before and after that point; you just shouldn't be accessing it.

This is a bit of a simplification, but consider the following:

Suppose I gave you a list of a characters on a slip of paper, labelled like so:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 ...
 a  x  5  0  t  t  t  t  u  m  2  1  0  .  4  '  e  v  e  i  ...

I tell you, "When I ask for a string, read from the number I give you until you hit a period."
Then I say, "Could you arrange for 6 characters of space to be available for me to strong something?"
You reply, "Sure, all the characters on here are garbage anyway. I'll give you space starting at 05."
Then I ask, "What is the string at 05?"
To answer this, you only need to know the rules for reading a string from an address, as I gave above. You start at the number and read until you hit a period; simple enough. So you answer "tttum210", as that starts at 05 and goes until the period at 14.

But wait, I only asked for 6 characters of space! The fact is, it doesn't matter. I asked you to read the string at 05 at the rules are clear on how to do that.

So back to your actual example, it doesn't make much sense to say "null char or not" as that is the only way to identify the end of a C-style string is by the null character; there string class (or anything else that handles C-strings) has no special way of taking the pointer to the beginning of the string and magically determining the size you allocated.

Hope this helps.
Topic archived. No new replies allowed.