(GO) const variable modify problem by pointer

Source: Internet
Author: User

Program Pen question-const variable passed pointer modification 2012-10-06 20:45:18

Category: C/

Const variables can be modified by pointers in certain situations, but in other cases they cannot be modified by pointers. The following is a test under VC6.1 conditions that cannot be modified#include <stdio.h>
int const A = 10;
void Main (void)
{
int *p = (int*) &a;
*p = 20;
printf ("%d\n", *p);
The program was compiled through, but run-time error: Indicates that a storage space can not be written, that is, no write permission, cannot modify its value. It is estimated to be stored in the global space and only readable properties.2 can modify the situation#include <stdio.h>
void Main (void)
{
int const A = 10;
int *p = (int*) &a;
*p = 20;
printf ("&a=%d\n", &a);
printf ("p=%d\n", p);
printf ("a=%d\n", a);
printf ("*p=%d\n", *p);
}The program works correctly and the constants are modified, but there is a problem:why printf ("a=%d\n", a);print a=10? is it possible for an address space to store two different values, of course not, haha, because A is a const variable, and the compiler replaces it with a during preprocessing. The compiler reads only one time for the value of a const variable. So the print is 10. A the actual stored value has changed. But why it can be changed, from its storage address can be seen, it is stored in the stack. Verify the following:#include <stdio.h>
void Main (void)
{
int const A = 10;
int B = 20;
int *p = (int*) &a;
*p = 20;
printf ("&a=%x\n", &a);
printf ("&b=%x\n", &b);
printf ("p=%x\n", p);
printf ("a=%d\n", a);
printf ("*p=%d\n", *p);
}the addresses of variables A and b are similar.

Summary, the const global variable is stored in the global storage space, its value is only readable property, cannot be modified;

The const local variable is stored in the stack, and its value can be modified by pointers;

The const variable is processed in preprocessing, and the compiler only reads its value once.

Answer the question.

Const in C is a moral guarantee that the value of a variable is not modified , and it cannot actually block the modification, and the value of the constant variable can be modified by a pointer, but there are some unknown results. In several cases, we look at it one by one.

1, directly assigned value

const int a = 3;a = 5;// const.c:6:2: error: assignment of read-only variable ‘a’

This situation does not have to say much, compile wrong.

2, using the pointer assignment, @ Sun Jianbo mentioned method, in the GCC in the warning,g++ error, because the code is not written, from non-const to const without explicit conversion, const to non-const requires an explicit conversion, This situation should use an explicit type conversion.

ConstIntA=3;int* b =  (int< Span class= "o" >*) &a; Printf ( "a =%d, *b =%d\n" , a*b); b = 5; Printf ( "a =%d, *b =%d\n" , a*b       

Run results (note: The results are consistent using MSVC compilation):

$ gcc const.c$ ./a.outa = 3, *b = 3a = 5, *b = 5$ g++ const.cpp$ ./a.outa = 3, *b = 3a = 3, *b = 5

When compiling with g++, the value of a is not changed because at compile time a is a constant and then compiled by the compiler into an immediate number. Therefore, using the B pointer modification does not change the value of a.

It is important to note that if a is defined as a global constant variable, using a pointer modification throws segment fault.

In the prototype of a function, we also use the const modifier pointer, which means that the implementation of the function is morally not going to modify the space that the pointer points to. For example, we are familiar with the strcpy function, the prototype is as follows:

char* strcpy(char* dst, const char* src);


The parameter src type passed in is const char*, which means that the internal implementation of the function does not modify the space pointed to by SRC. The moral is that the value of the space can be modified internally by forcing the type conversion through the pointer above. Also, if we declare that we do not modify the space pointed to by the incoming pointer, then we should not modify the space, because the incoming pointer may be a non-writable memory, and then a segment error occurs.

(GO) const variable modify problem by pointer

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.