I seem to be able to write as many characters as I want into "buffer" even though I declare it as length 1 - why is this?

string teststring = "teststring";
	cout << teststring << endl;

	char buffer[1];
	itoa(34567,buffer,10);

	teststring.append(buffer);
	cout << teststring << endl;

Thanks!

Dave

Recommended Answers

All 2 Replies

uhm, not exactly sure, but mabey the itoa function automaticly allocates the appropriate memory?

>why is this?
Dumb luck. You happened to write to memory that isn't critical to the operation of the program. Therefore, it doesn't crash immediately, only silently corrupts whatever was there. Whether it seems to work or not, you're always taking a huge risk by overflowing your memory.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.