Let’s say you have a string like this:
char* a="01234"
Letting &a=0000, &(a+1)=0001, &(a+2)=0002, and so on, what happens if there is some memory already allocated for another variable at 0007, and you try resizing a to a larger size (a=”0123456789″)? Will a move to a new location that can fit the larger string, or will the data in 0007 be overwritten?
Edit:
I don’t understand what people mean when they say resizing C-style strings is not allowed.
char* a="01234"
cout << &a; //Prints 00FF1360
cout << a; //Prints 01234
a="0123456789"
cout << &a; //Still prints 00FF1360
cout << a; //Prints "0123456789"
The pointer a didn’t change at all when I reassigned it. It seems like the original string “01234” was just destroyed and “0123456789” replaced it in the same location.
Nevermind, I was printing the location of the pointer rather than the location it was pointing to. As a side question, does anyone know how to print a character pointer’s value without it printing the string instead?
What you’ve written isn’t C++, and it’s definitely not allowed.
Correct way:
You can only read the values
a[0]toa[5], but never change them. You can change the valuesb[i]for i in 0 to 5, but be sure not to setb[5]to anything but zero.Any access, read or write, to
a[i]orb[i]withigreater than five is undefined behaviour and almost definitely suicidal.In the case of
a, the string literal usually lives in a global, read-only part of memory. On the other hand,bis just an automatic array in the local scope that’s initialized to the given six characters (including terminal zero).Conversely, if by some mysterious ways you have already come at the address of some valid area of memory, you could wreak all sorts of havoc:
Indeed C and C++ let you freely roam about your process’s virtual memory space, but you may get shot at any point!
(For you second question, writing
a = "56789";is fine, but it will setato point to a different string and not modify the original one (andashould still be of typeconst char *).)