C ++ const pointer with operator []

char* const p = "world"; p[2] = 'l'; 

The first statement creates the line indicated by the const p pointer, and the second statement tries to change the line, and it is accepted by the compiler, while an access violation exception pops up during operation, and can anyone explain why?

+4
source share
5 answers

So your question is double:

  • Why this gives access violation: character literal strings are stored as literals on the CODE pages of your executable program; the most modern operating system does not allow changing these pages (including MS-windows), thus, protection.

  • Why does the compiler allow this: the const keyword in this context refers to a pointer, and not to what it points to. Code such as p = "Hello"; will result in a compiler error since you declared p as a constant (not * p). If you want to declare an item that it indicates as permanent, then your declaration should be const char * p.

+3
source

IN

  char* const p = "World"; 

P points to an array of const characters that is in the .rodata memory area. Thus, you cannot change the data specified by the variable p, and you cannot change p to point to another line.

+3
source
 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 outdated, and a good compiler with the correct warning level should warn about this.

The reason is that the literal type "world" is equal to char const[6] . In other words, the literal is always constant and cannot be changed. When you speak...

 char* const p = "world"; 

... then the compiler will convert the literal to a pointer. This is done implicitly by an operation called "array decay": the C array can be implicitly converted to a pointer pointing to its beginning.

So, "world" converted to a value of type char const* . Pay attention to const - we are not yet allowed to change the literal, even if it is accessible through a pointer.

Alas, C ++ 03 also allows you to assign literals to a non- const pointer to provide backward compatibility with C.

Since this is an interview question, the correct answer is this: the code is illegal and the compiler must not allow it. Heres the corrected code:

 char const* const p = "world"; //p[2] = 'l'; // Not allowed! 

We used two const here: the literal requires the first. The second one is the pointer itself (and not the pointed value) const .

+1
source

The point is that you define a string literal as follows:

 char * p = "some text" 

the compiler will prepare the memory for the pointer only, and the text location will be set to the .text section by default

on the other hand, if you define it as:

 char p[] = "some text" 

the compiler will know that you need memory for the entire array of characters.

In the first case, you cannot read the value, since the MMU is configured for read-only access for the .text section in memory, in the second case, you can freely access and change the memory.

Another opinion (to prevent a run-time error) would be the correct description of the memory pointed to by the pointer.

For a constant pointer, this will be:

 const char * const p = "blaaaah" 

and for normal:

 const char * p = "blaah" 
0
source
 char* const p = "world"; 

Here p is a pointer with a constant memory address. This way, this will compile fine, since there is no syntax error in the compiler, and it throws an exception in runtime bcoz as a constant pointer, you can change its value.

C ++ link constant pointers

Error correctness

-2
source

Source: https://habr.com/ru/post/1501857/


All Articles