Reputation: 649
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?
Upvotes: 3
Views: 1197
Reputation: 168646
@albert, maybe this program will help you understand what is going on.
#include <iostream>
int main() {
// Create an anonymous array of 6 bytes *somewhere* and
// point "a" at the first byte.
const char *a = "01234";
// Display the string:
std::cout << a << "\n";
// Display that value of the pointer
std::cout << (const void*)a << "\n";
// Create an anonymous array of 8 bytes somewhere else and
// point "a" at the first byte.
a = "0123456";
std::cout << a << "\n";
std::cout << (const void*)a << "\n";
// Note well: only the pointer changed. Neither anonymous array changed.
// Create an array of 4 bytes named "b"
char b[] = "012";
// point a at it:
a = b;
std::cout << a << "\n";
std::cout << (const void*)a << "\n";
std::cout << (void*)b << "\n";
}
Upvotes: 0
Reputation: 477120
What you've written isn't C++, and it's definitely not allowed.
Correct way:
const char * a = "01234"; // pointer to array of six const chars.
char b[] = "01234"; // array of six chars
You can only read the values a[0]
to a[5]
, but never change them. You can change the values b[i]
for i in 0 to 5, but be sure not to set b[5]
to anything but zero.
Any access, read or write, to a[i]
or b[i]
with i
greater 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, b
is 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:
char * evil = use_dastardly_exploit();
memcpy(evil, my_leet_shellcode, 1024);
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 set a
to point to a different string and not modify the original one (and a
should still be of type const char *
).)
Upvotes: 4
Reputation: 42642
You have to resize the array a
points to if you want it to contain more chars. Assigning a larger string won't automatically resize the underlying array (instead it would overwrite other variables or memory not belonging to the program at all). But if you do resize the array (using realloc for example) it may move the array to a new memory location.
Upvotes: 0
Reputation: 20859
Under most circumstances the question should not be "Are C-Like Strings dangerous ?
". The question should be "What are the reasons for not using stl strings
" (std::string).
There is also a pretty similar discussion here: Is the function strcpy always dangerous?
Upvotes: 0
Reputation: 2181
It will be overwritten. Use std::string
if you need dynamic length strings.
Upvotes: 0
Reputation: 42627
There is no resizing if you reassign a to something else; they are pointers to different memory locations. (And in the case of constant strings like the above, they are immutable read-only strings.)
Upvotes: 4
Reputation: 38798
c-style strings like that are just arrays. Arrays allocated via malloc can be resized using realloc, but those assigned like that aren't. Accessing memory outside the string is accessing memory that could be used by something else, or is uninitialized, or otherwise bad.
Upvotes: 5