Too much memory allocated via new char[]?

Hello folks!

While working on an exercise from my C++ Primer Plus book, I attempted to program a dynamic char array that would size itself to fit a string perfectly, but the array ended up bigger than expected. The program executes fine, but any insight into why the array is 13 characters larger than string would be appreciated!

Note: The exercise provided the program framework and specific instructions, so I recognize there the call to new that is never deleted and that the using char arrays instead of strings is a bit whacky. Oh well!

1
2
3
4
5
6
7
struct stringy {
	char * str;			// points to a string
	int ct;				// length of string (not counting '\0')
};

// prototypes for set() ... go here
void set(string & myStringy, char * str);


1
2
3
4
5
6
7
8
9
int main()
{
        ...
        stringy beany;
        char testing[] = "Reality isn't what it used to be.";
        set(beany, testing);
        ...
        return 0;
}


1
2
3
4
5
6
7
8
9
// Note that set() should use new to allocate sufficient space to hold
// the designated string.
void set(stringy & myStringy, char * str)
{
	myStringy.ct = strlen(str);
	myStringy.str = new char[myStringy.ct + 1];
        // cout << "TEST LENGTH: " << strlen(myStringy.str);
	strcpy(myStringy.str, str);
}


What confuses me is the line: myString.str = new char[myString.ct + 1].
main() passes a C-style string with 33 characters, and that size is copied to myStringy.ct. The program then creates a new array using that size + 1, but for some reason the resulting string assigned to myStringy.str is 46 chars long instead of 34. I don't know where extra 13 characters came from.

I was originally concerned that I'd accidentally lop off the end of the string if I forgot to add one to the end
( = new char[mystringy.ct + 1] ), but I never got close to running out of room.
Last edited on
I think you are confusing two different quantities.
The number of bytes allocated will be myStringy.ct + 1 because that's what you requested (unless for some reason the allocation fails).

However, when you use the strlen() function it doesn't tell you anything about the length of the allocated block. It simply counts how many non-zero characters it found before hitting a null terminator.

If you have attempted to use strcpy() to copy a string which is longer than the allocated block, you are basically writing to an area of memory which your program has no right to be using. You may think the program is working just fine, but such usage is invalid and is corrupting areas of memory being used for some other purpose, which will cause unpredictable behaviour,most likely crashing the program.
Thanks for the response.

If I understand correctly, the line myStringy.str = new char[myStringy.ct +1]; is creating an array of the appropriate size, but my attempt to check up on the size with strlen() is incorrect because strlen() may count extra junk beyond the allocated memory before it encounters a null character to terminate it.

If that's the case, then it makes sense why calling strcpy(myStringy.str, str) and then checking strlen() afterwards worked correctly, because strcpy() forcefully inserted a null character at the end.

I think I understand better (hopefully. Much obliged, Chervil!
Topic archived. No new replies allowed.