I've been implementing a simplified string class for a series of exercises. I'm asked to add a c_string() function, so I have this experimental member function:
What's confusing me is that the program works even though I don't specify a size for the array of char (I know I should btw!). I'm able to write to the new char and beyond without any complaints or crashes, as long as the text is only two or three words. Any more than this and I get a bad alloc message and it crashes. Is it that I'm allowed to keep writing to successive addresses until I hit memory that's already allocated? I didn't think you were allowed to write to memory unless it was specifically allocated in full.Code:char *c_string() { if(c_str) delete c_str; char *c_str = new char; char *temp = c_str; for (size_type i = 0; i < data.size(); i++) *(temp++) = data[i]; *temp = '\0'; return c_str; }