Remove const-ness from a variable - c++

i'm trying to remove const-ness from a variable (char*), but for some reason when i try to change the value, the original value of the const variable still remains the same.
const char* str1 = "david";
char* str2 = const_cast<char *> (str1);
str2 = "tna";
now the value of str2 changes but the original value of str1 remains the same, i've looked it up on Google but couldn't find a clear answer.
when using const_cast and changing the value, should the original of the const variable change as well ?

The type of str1 is const char*. It is the char that is const, not the pointer. That is, it's a pointer to const char. That means you can't do this:
str1[0] = 't';
That would change the value of one of the const chars.
Now, what you're doing when you do str2 = "tna"; is changing the value of the pointer. That's fine. You're just changing str2 to point at a different string literal. Now str1 and str2 are pointing to different strings.
With your non-const pointer str2, you could do str2[0] = 't'; - however, you'd have undefined behaviour. You can't modify something that was originally declared const. In particular, string literals are stored in read only memory and attempting to modify them will bring you terrible misfortune.
If you want to take a string literal and modify it safely, initialise an array with it:
char str1[] = "david";
This will copy the characters from the string literal over to the char array. Then you can modify them to your liking.

str2 is simply a pointer. And your code just changes the value of the pointer, the address, not the string to which it points.
What's more, what you are attempting to do leads to undefined behaviour, and will most likely result in runtime errors. All modern compilers will store your string "david" in read-only memory. Attempts to modify that memory will lead to memory protection errors.

Related

C++ different behaviour when trying to assign to const types [duplicate]

Why does the following code in C work?
const char* str = NULL;
str = "test";
str = "test2";
Since str is a pointer to a constant character, why are we allowed to assign it different string literals? Further, how can we protect str from being modified? It seems like this could be a problem if, for example, we later assigned str to a longer string which ended up writing over another portion of memory.
I should add that in my test, I printed out the memory address of str before and after each of my assignments and it never changed. So, although str is a pointer to a const char, the memory is actually being modified. I wondered if perhaps this is a legacy issue with C?
You are changing the pointer, which is not const (the thing it's pointing to is const).
If you want the pointer itself to be const, the declaration would look like:
char * const str = "something";
or
char const * const str = "something"; // a const pointer to const char
const char * const str = "something"; // same thing
Const pointers to non-const data are usually a less useful construct than pointer-to-const.
Further, how can we protect str from being modified?
char * const str1; // str1 cannot be modified, but the character pointed to can
const char * str2; // str2 can be modified, but the character pointed to cannot
const char * const str3 // neither str3 nor the character pointed to can be modified.
The easiest way to read this is to start from the variable name and read to the left:
str1 is a constant pointer to a character
str2 is a pointer to a character constant
str3 is a constant pointer to a character constant
NOTE: the right-to-left reading does not work in the general case, but for simple declarations it's a simple way to do it. I found a java applet based on code from "The C Programming Language" that can decipher declarations with a full explanation of how to do it.
On a related note, definitely take a look at "const pointer versus pointer to const". It helps with what some people call const correctness. I keep it in my bookmarks so that I can refer to it every now and then.
What you're looking for may be the syntax...
const char* const str = NULL;
str = "test";
str = "test2";
Notice the "const" after the char* which yields a compiler error when trying to compile/build.
Memory for the string literals are allocated on the stack, and all your assignments do are change the str pointer to point to those memory addresses. The constant character it pointed to initially hasn't changed at all.
Besides, declaring a variable as const means that variable is read-only; it does not mean the value is constant!

Is there a dangling pointer problem in this code?

string str;
char *a=str.c_str();
This code is working fine for me but every place else I see this code instead
string str;
char *a=new char[str.length()];
strcpy(a,str.c_str());
I wonder which one is correct and why?
Assuming that the type of str is std::string, neither of the code is are correct.
char *a=str.c_str();
is invalid because c_str() will return const char* and removing const without casting (usually const_cast) is invalid.
char *a=new char[str.length()];
strcpy(a,str.c_str());
is invalid because str.length() don't count the terminating null-character while allocating for terminating null-character is required to use strcpy().
There are no dangling pointer problem in code posted here because no pointers are invalidated here.
The two code segments do different things.
The first assigns the pointer value of str to your new c-tpye string, and implicitly converts from const char*(c_str() return type) to char*, which is wrong. If you were to change your new string you would face an error. Even if c_str() returned char*, altering the new string would also make changes in str.
The second on the other hand creates a new c-type string from the original string, copying it byte-by-byte to the new memory allocated for your new string.
Although the line of code you wrote is incorrect, as it does not cover the terminating null character of a c-type string \0. In order to fix that, allocate 1 extra byte for it:
char *a=new char[str.length()+1];
After copying the data from the first string to your new one, making alterations to it will not result in changes in the original str.
Possibly.
Consider this.
char const* get_string() {
string str{"Hello"};
return str.c_str();
}
That function returns a pointer to the internal value of str, which goes out of scope when the function returns. You have a dangling pointer. Undefined behaviour. Watch out for time-travelling nasal monkeys.
Now consider this.
char const* get_string() {
string str{"Hello"};
char const* a = new char[str.length()+1];
strcpy(a, str.c_str());
return a;
}
That function returns a valid pointer to a valid null-terminated C-style string. No dangling pointer. If you forget to delete[] it you will have a memory leak, but that's not what you asked about.
The difference is one of object lifetime. Be aware of scope.

C++ string literal type and declaration

Two types of the declaration:
char* str1 = "string 1";
and
char str2[] = "string 2";
my compiler doesn't allow me to use first declaration with a error
incorrect conversion from const char[8] to char*. looks okay,
the version like this:
const char* str1 = "string 1";
passed by compiler.
please clarify my understanding.
I believed that if we declare both versions e.g. in main(),
first one (const char*) - the only pointer will be allocated on the stack and initialized with some address in data segment.
second version (char[]) - whole array of symbols will be placed on the stack
as far as I see string literal does now always have a const char[] type.
is a using of const char* depricated? for C compatibility only?
where each version will store the string ?
char* str1 = "string 1";
This is deprecated as of C++98, and is ill-formed as of C++11, as a string literal can't be modified. Modifying it would result in undefined behavior.
To avoid this, the standard prohibits assigning it to a modifiable char pointer, as it might be modified later on without the programmer realizing that he/she shouldn't have modified it.
char str2[] = "string 2";
Yes, this allocated an array of characters, whereas each character is stored on the stack.
const char* str1 = "string 1";
This isn't deprecated, it is the recommended way (only way) to assign a string literal to a char pointer. Here, str1 is points to const chars, i.e. they can't be modified. Thus it is safe to use it somewhere, as the compiler will enforce that the chars will never be modified.
Here, str1 is stored on the stack, pointing to a string literal, which may or may not be stored in read-only memory (this is implementation defined).
char str2[] = "string 2";
"string 2" is string literal const char[9] stored in read-only memory.
char str2[] will allocate char array (of size deducted from initializer size) in read-write memory.
= will use the string literal to initialize the char array (doing memcpy of the "string 2" content).
I mean in principle. The actual machine code produced with optimizations may differ, setting up str2 content by less trivial means than memcpy from string literal.
char* str1 = "string 1"; - here you are trying to get the actual string literal memory address, but that one is const, so you shouldn't assign/cast it to char *.
const char* str1 should work OK, casting from const char[] to const char * is valid (they are almost the same thing, unless you have access to original array size during compilation, then the pointer variant is size-less dumb-down version).

in c++ is this a good practice to initialize char array with string literal?

in c++ is this a good practice to initialize char array with string?
such as:
char* abc = (char *) ("abcabc");
I see a lot of these in my co-worker's code. Should I change it to the right practice?
such as
std::string abc_str = "abcabc";
const char* abc= abc_str .c_str();
This statement
char* abc = (char *) ("abcabc");
is simply bad. String literals in C++ have types of constant character arrays. So a valid declaration will look like
const char *abc = "abcabc";
Note: In C you indeed may write
char *abc = "abcabc";
Nevertheless string literals are immutable. Any attempt to modify a string literal results in undefined behaviour.
By the way there is no any character array that is initialized by a string literal.:) Maybe you mean the following
char abc[] = "abcabc";
Using standard class std::string does not exclude using character arrays and moreover pointers to string literals.
Take into account that these declarations
const char *abc = "abcabc";
and
std::string abc_str = "abcabc";
const char* abc= abc_str .c_str();
are not equivalent. Relative to the first declaration string literals have static storage duration and their addresses are not changed during the program execution.
In the second declaration pointer abc points to dynamically allocated memory that can be reallocated if object abc_str will be changed. In this case the pointer will be invalid.
Also the first declaration supposes that the array (string literal) pointed to by the pointer will not be changed. In the second declaration it is supposed that the object of type std::string will be changed. Otherwise there is no great sense to declare an object of type std::string instead of the pointer.
Thus the meanings of the declarations are simply different.
char* abc = (char *) ("abcabc");
That is bad. Don't do it.
You are treating a string literal that is not supposed to be modified like it can be modified.
After that,
abc[0] = 'd';
will be OK by the compiler but not OK at run time. What you need to use is:
char abc[] = "abcabc";
This will create an array that is modifiable.
Both of those are bad.
char* abc = (char*) ("abcabc");
A string literal is a constant and, as such, may be stored in write protected memory. Therefore writing to it can crash your program, it is undefined behaviour.
Rather than cast away the constness you should keep it const and make a copy if you want to edit its contents.
const char* abc = "abcabc";
The other one should be avoided too:
std::string abc_str = "abcabc";
const char* abc = abc_str.c_str();
Keeping it const is good but if the string is changed it could be reallocated to another place in memory leaving your pointer dangling.
Also in pre C++11 code the pointer stops being valid the second it is assigned because there is no guarantee it is not a temporary.
Better to call abc_str.c_str() each time.
The chances are that because c_str() is such a trivial operation it will be optimized away by the compiler making it just as efficient as using the raw pointer.
Instead of both of those what you should be doing is using std::string all the way. If you absolutely need a const char* (for old legacy code) you can obtain it using c_str().
std::string abc_str = "abcabc"; // this is perfect why do more?
old_horrible_function(abc_str.c_str()); // only when needed

Pointer to const char vs char array vs std::string

Here I've two lines of code
const char * s1 = "test";
char s2 [] = "test";
Both lines of code have the same behavior, so I cannot see any difference whether I should prefer s1 over s2 or vice-versa. In addition to s1 and s2, there is also the way of using std::string. I think the way of using std::string is the most elegant. While looking at other code, I often see that people either use const char * or char s []. Thus, my question is now, when should I use const char * s1 or char s [] or std::string? What are the differences and in which situations should I use which approach?
POINTERS
--------
char const* s1 = "test"; // pointer to string literal - do not modify!
char* s1 = "test"; // pointer to string literal - do not modify!
// (conversion to non-const deprecated in C++03 and
// disallowed in C++11)
ARRAYS
------
char s1[5] = "test"; // mutable character array copied from string literal
// - do what you like with it!
char s1[] = "test"; // as above, but with size deduced from initialisation
CLASS-TYPE OBJECTS
------------------
std::string s1 = "test"; // C++ string object with data copied from string
// literal - almost always what you *really* want
const char * s1 = "test";
char s2 [] = "test";
These two aren't identical. s1 is immutable: it points to constant memory. Modifying string literals is undefined behaviour.
And yes, in C++ you should prefer std::string.
The first one is constant, the second isn't. std::string is a class type and implements many useful functions and methods for string manipulation, making it much easier and user-friendly. The c-style 'strings' with char pointers are difficult to control, manipulate and often cause errors, but don't have the overhead the std::string has. Generally it's better to stick to the std::strings cause they're easier to maintain.
The only difference between the two that you should care about is this:
Which one is your project already using?
These two do not have the same behavior. s1 is a simple pointer which is initialized to point to some (usually read-only) area of the memory. s2, on the other hand, defines a local array of size 5, and fills it with a copy of this string.
Formally, you are not allowed to modify s1, that is, do something like s1[0] = 'a'. In particular, under weird circumstances, it could cause all other "test"s in your program to become "aest", because they all share the same memory. This is the reason modern compilers yell when you write
char* s = "test";
On the other hand, modifying s2 is allowed, since it is a local copy.
In other words, in the following example,
const char* s1 = "test";
const char* s2 = "test";
char s3[] = "test";
char s4[] = "test";
s1 and s2 may very well point to the same address in memory, while s3 and s4 are two different copies of the same string, and reside in different areas of memory.
If you're writing C++, use std::string unless you absolutely need an array of characters. If you need a modifiable array of characters, use char s[]. If you only need an immutable string, use const char*.
Use std::string unless you know why you need a char array / pointer to char.
which one to be used depends upon your requirement. Pointer offers you more flexiblity. and in some cases vulerability. Strings are a safe option and they provide Iterator support.