Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is resizing strings in C++ dangerous

Tags:

c++

string

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?

like image 614
john smith Avatar asked Jan 25 '26 06:01

john smith


2 Answers

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.

like image 160
Herms Avatar answered Jan 27 '26 23:01

Herms


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 *).)

like image 20
Kerrek SB Avatar answered Jan 28 '26 01:01

Kerrek SB