I was doing some some string manipulation and encountered with a problem.
char *arr1 = "HELLO";
(*arr1)++;
this throws an error "Access violation writing to location"!
however, below code works fine.
char arr1[] = "HELLO";
(*arr1)++;
and what are their memory segments on which both the char* arr1 and char arr1[] are stored ?
char *arr1 = "HELLO";
The above definition implies that the memory for arr1 could be allocated in a read-only part of memory (it is implementation-defined behaviour, actually), and thus could cause 'Access violation' when you are trying to change the value at the memory location is pointing to.
char arr1[] = "HELLO";
In this case, the memory for arr1 is allocated in stack - which is writable. Hence, the expression (*arr1)++ works fine without any issue.
In the first case, arr1 is a local variable which holds a pointer to a read-only memory segment containing the string "HELLO". The statement (*arr1)++ tries to modify the first byte of the segment (which contains the character 'H'), which leads to access violation.
In the second case, arr1 is a local variable which holds an array of six bytes, which are initialized with {'H', 'E', 'L', 'L', 'O', 0}. Local variables are in read-write memory, so modifying them doesn't lead to an error.
There is no pointer arithmetic. You are adding one to the first charachter of the string. Since string constants are not writable, the first version gives an error. In the second version you change the content of on char array, so there is no problem.
You didn't do any pointer arithmetic. You dereferenced the pointer then incremented the thing it points to (the first character).
Unfortunately for you, string literals cannot be modified (and you should have been warned by your compiler that char* is bad and const char* is good, for pointing to string literals).
It works in the second case because initialising a local array from a string literal creates your own copy of the data.
Literals like "HELLO" are of type const char[] which decays to const char*, compiler can put it in memory which actually cannot be modified, modification results in undefined behaviour. g++ gives you warning like below:
main.cpp:7:14: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
char *arr1 = "HELLO";
Compiler allows this because as your can read here (http://en.cppreference.com/w/cpp/language/string_literal):
In C, string literals are of type char[], and can be assigned directly
to a (non-const) char*. C++03 allowed it as well (but deprecated it,
as literals are const in C++). C++11 no longer allows such assignments
without a cast.
but actually I dont get errors on g++ even with C++11 enabled.
arrays of course you can modify, so your second example works fine. This code:
char arr1[] = "HELLO";
creates on stack an array of length 6 and initializes it with "HELLO\0"
Related
I was trying strcpy like this:
int main()
{
char *c="hello";
const char *d="mello";
strcpy(c,d);
cout<<c<<endl;
return 0;
}
Compiling this gives a warning and running the code produces a Segmentation fault.
The warning is:
warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
char *c="hello";
The declaration of strcpy is: char * strcpy ( char * destination, const char * source );
So where am I wrong (regarding the warning)? IMO I have used a const char and a char, same as the function's declaration.
Does c* or d* not allocate memory to hold "hello" and "mello", due to
which it is throwing segmentation fault? How does the
initialization/definition of a variable like c* work?
Both variables are pointing to constant literal text.
Modifying constant literal text is undefined behavior.
Place the text into an array if you want to modify it:
char e[] = "fred";
e[0] = 'd';
Compiler warnings (and errors) always refer to a particular line of code (and a portion of that line as well). It's a good idea to pay close attention to where the problems are.
In your case, the warning is not about calling strcpy, but about initialising c. You're making a char *, a pointer to modifiable char, point to "hello", a read-only string literal. That's what the compiler is warning you about.
This used to be possible in C++, but with the caveat that you must never actually modify the memory pointed to (since string literals are immutable). Since C++11, converting a string literal to a char * is expicitly forbidden and will generate an error instead of a warning.
The reason for the segfault should be clear now: you're trying to overwrite a piece of read-only memory with the strcpy call.
The correct solution is to use writable memory for storing the destination string. You could do it like this:
char c[] = "hello";
This creates a new char array, which is writable normally (just like any other local variable), and initialises the array with the contents of the string literal (effectively a copy thereof).
Of course, since this is C++, you should be using std::string to store strings and not bother with char*, strcpy, and other C-isms at all.
The code you are trying is for TurboC++ it seems. As drescherjm pointed out in the comments, the char *c="hello"; is no longer legal in c++.
If you try out your code in Turbo C++, it'll work just as you expected, although this isn't the same case with the modern c++.
So where am I wrong (regarding the warning)? IMO I have used a const char and a char, same as the function's declaration.
The warning is due to the reason mentioned above, plus the warning isn't on the use of strcpy, it's on the declaration.
Does c* or d* not allocate memory to hold "hello" and "mello", due to which it is throwing segmentation fault? How does the initialization/definition of a variable like c* work?
Well, the compiler would just put the strings in to a random place in the memory and let the c/d pointer point to it. It's risky as you can lose the data if you make the pointer point to something else by accident.
What happen when constant string assigned to constant character pointer(or character pointer)? ex:
const char* p="String";
how and where the compiler take this array .. heap memory ?
and what different from it and :
char* p="String";
thanks.
What happen when constant string assigned to constant character pointer(or character pointer)?
Nothing happens to the const string itself: a pointer to it is assigned to p, that's all.
how and where the compiler take this array .. heap memory?
It does not take it anywhere. String's data remains where it was, which is a compiler-specific thing.
and what different from it and : char* p="String";
The compiler is going to reject a program with the assignment of a literal to non-const, or warn you of a deprecated conversion, depending on the C++ version and/or compiler settings.
If you try to modify p[...]'s content using the const declaration, the compiler is going to stop you. If you try doing the same without const, the program may compile, bit it would cause undefined behavior at runtime.
The string literal "String" is a static array of const char somewhere in your program, probably placed into a read-only part of the address space when the executable is set up by your OS.
When you assign const char *p = "String", then p is initialized with a pointer to that array of const char. So *p is 'S' and p[1] is 't', etc.
When you assign char *p = "String", then your compiler should reject that (perhaps you have insufficient diagnostic level set?). If you tell the compiler to accept it regardless, then you have a pointer to (modifiable) char pointing at the string literal. If you subsequently attempt to write through this pointer, you'll get no compiler error, and instead you are likely to see one of two problems runtime:
(If the compiler/linker has placed the string literal into read-only memory) a signal is raised indicating memory access violation (SIGSEGV on Unix-like systems).
(If the string literal is in writeable memory) other uses of the same string literal get modified, because the compiler is permitted to point them all at the same storage.
The nearest question on this website to the one I have had a few answers that didn't satisfy me.
Basically, I have 2 related questions :
Q. If I do something like :
char a[]= "abcde"; //created a string with 'a' as the array name/pointer to access it.
a[0]='z'; //works and changes the first character to z.
But
char *a="abcde";
a[0]='z'; //run-time error.
What is the difference? There is no "const" declaration, so I should be free to change contents, right?
Q. If I do something like :
int i[3];
i[0]=10; i[1]=20; i[2]=30;
cout<<*++i; //'i' is a pointer to i[0], so I'm incrementing it and want to print 20.
This gives me a compile-time error, and I don't understand why.
On the other hand, this works :
int *i=new int[3];
i[0]=10; i[1]=20; i[2]=30;
cout<<*++i; //Prints 20.
Thanks for the help.
Q
char *a="abcde";
a[0]='z'; //run-time error.
Ans - Here a is pointing to string literal stored in read only location. You cannot modify string literal
Q
int i[3];
i[0]=10; i[1]=20; i[2]=30;
cout<<*++i;
Ans- Array and Pointers are not same thing. Here i is not a pointer.
You need lvalue for increment operand
You can do :
int *p = &i[0];
std::cout<<*++p;
In last case operator new returns a pointer to a allocated memory space, so its possible.
Question 1
The trouble is that it is still const.
A string literal actually has the type char const*.
But when they designed C++03 they decided (unfortunately) that conversion from char const* to char* is not an error. Thus the code:
char *a="abcde";
Actually compiles without error (even though the string is const). BUT the string literal is still a const even though it is being pointed at via a non const pointer. Thus making it very dangerous.
The good news is that most compilers will generate a warning:
warning: deprecated conversion from string constant to ‘char*’
Question 2
cout<<*++i; //'i' is a pointer to i[0], so I'm incrementing it and want to print 20.
At this point i is not a pointer. It's type is still an array.
It is not a valid operation to increment array.
The thing you are thinking about; is that arrays decay into pointers at the drop of a hat (like when you pass them to functions). Unfortunately that is not happening in this situation and thus you are trying to increment an array (which does not make sense).
char* const p = "world";
p[2] = 'l';
The first statement creates a string pointed by a const pointer p, and the second statement tries to modify the string, and it is accepted by the compiler, while in the running time, an access violation exception is poped, and could anyone explain why?
So your question is two-fold:
Why does it give an access violation: character literal strings are stored as literals in the CODE pages of your executable program; most modern operating system do not allow changes to the these pages (including MS-windows) thus the protection fault.
Why does the compiler allow it: the const keyword in this context refers to the pointer and not the thing it points at. Code such as p="Hello"; will cause a compiler error as you have declared p as constant (not *p). If you wanted to declare the thing it points to as constant then your declaration should be const char *p.
In the
char* const p = "World";
P points to const character array, which resides in the .rodata memory area. So, one can not modify the data pointed by the p variable and one cannot change the p to point to some other string as well.
char* const p = "world";
This is illegal in the current C++ standard (C++11). Most compilers still accept it because they use the previous C++ standard (C++03) by default, but even there the code is deprecated, and a good compiler with the right warning level should warn about this.
The reason is that the type of the literal "world" is char const[6]. In other words, a literal is always constant and cannot be changed. When you say …
char* const p = "world";
… then the compiler converts the literal into a pointer. This is done implicitly by an operation called “array decay”: a C array can be implicitly converted into a pointer that points to its beginning.
So "world" is converted into a value of type char const*. Notice the const – we still are not allowed to change the literal, even when accessed through the pointer.
Alas, C++03 also allows that literals be assigned to a non-const pointer to provide backwards compatibility with C.
Since this is an interview question, the correct answer is thus: the code is illegal and the compiler shouldn’t allow it. Here’s the corrected code:
char const* const p = "world";
//p[2] = 'l'; // Not allowed!
We’ve used two consts here: the first is required for the literal. The second makes the pointer itself (rather than the pointed-to value) const.
The thing is if you define a string literal like this:
char * p = "some text"
the compiler will prepare memory for pointer only, and the text location
will be by default set to .text section
in the other hand if you define it as:
char p[] = "some text"
the compiler will know that you want the memory for entire character array.
In the first case you can't read the value as the MMU is set to readonly access for the .text section of memory, in the 2nd case you can freely access and modify memory.
Another think (to prevent the runtime error) would be correct describing of the memory the pointer points to.
For the constant pointer it would be:
const char * const p = "blaaaah"
and for the normal one:
const char * p = "blaah"
char* const p = "world";
Here p is a pointer with constant memory address. So this will compile fine as there is no syntax error as per compiler and it throws exception in runtime bcoz as its a constant pointer you can change its value.
C++ reference constant pointers
Const correctness
I have a few questions I would like to ask about string literals and C-strings.
So if I have something like this:
char cstr[] = "c-string";
As I understand it, the string literal is created in memory with a terminating null byte, say for example starting at address 0xA0 and ending at 0xA9, and from there the address is returned and/or casted to type char [ ] which then points to the address.
It is then legal to perform this:
for (int i = 0; i < (sizeof(array)/sizeof(char)); ++i)
cstr[i] = 97+i;
So in this sense, are string literals able to be modified as long as they are casted to the type char [ ] ?
But with regular pointers, I've come to understand that when they are pointed to a string literal in memory, they cannot modify the contents because most compilers mark that allocated memory as "Read-Only" in some lower bound address space for constants.
char * p = "const cstring";
*p = 'A'; // illegal memory write
I guess what I'm trying to understand is why aren't char * types allowed to point to string literals like arrays do and modify their constants? Why do the string literals not get casted into char *'s like they do to char [ ]'s? If I have the wrong idea here or am completely off, feel free to correct me.
The bit that you're missing is a little compiler magic where this:
char cstr[] = "c-string";
Actually executes like this:
char *cstr = alloca(strlen("c-string")+1);
memcpy(cstr,"c-string",strlen("c-string")+1);
You don't see that bit, but it's more or less what the code compiles to.
char cstr[] = "something"; is declaring an automatic array initialized to the bytes 's', 'o', 'm', ...
char * cstr = "something";, on the other hand, is declaring a character pointer initialized to the address of the literal "something".
In the first case you are creating an actual array of characters, whose size is determined by the size of the literal you are initializing it with (8+1 bytes). The cstr variable is allocated memory on the stack, and the contents of the string literal (which in the code is located somewhere else, possibly in a read-only part of the memory) is copied into this variable.
In the second case, the local variable p is allocated memory on the stack as well, but its contents will be the address of the string literal you are initializing it with.
Thus, since the string literal may be located in a read-only memory, it is in general not safe to try to change it via the p pointer (you may get along with, or you may not). On the other hand, you can do whatever with the cstr array, because that is your local copy that just happens to have been initialized from the literal.
(Just one note: the cstr variable is of a type array of char and in most of contexts this translates to pointer to the first element of that array. Exception to this may be e.g. the sizeof operator: this one computes the size of the whole array, not just a pointer to the first element.)
char cstr[] = "c-string";
This copies "c-string" into a char array on the stack. It is legal to write to this memory.
char * p = "const cstring";
*p = 'A'; // illegal memory write
Literal strings like "c-string" and "const cstring" live in the data segment of your binary. This area is read-only. Above p points to memory in this area and it is illegal to write to that location. Since C++11 this is enforced more strongly than before, in that you must make it const char* p instead.
Related question here.