Modifying the value of a const protection

Source: Internet
Author: User

Look at the code first:

#include <stdio.h>

void Main ()

{

const int num = 10;int *a = (int *) (void *) &num; Give the address to A*a = 20; The address is assigned a value of 20printf ("*a=%d\t num=%d\n", *a,num);

}

Here, by forcing the type conversion (int *) (void *), &num can actually change the value in memory, but for NUM, the compiler has processed it as a constant, so you can see the assembly (the reply provided by the Great God).

By looking at the assembly of printf:

printf ("*a =%d\t num =%d\n", *a,num);
009717BE Push 0Ah
009717C0 mov Eax,dword ptr [a]
009717C3 mov ecx,dword ptr [eax]
009717C5 push ECX
009717C6 Push 976b30h
009717CB call _printf (0971320h)
009717d0 Add esp,0ch

As you can see, push 0Ah here is a constant in the press.

Modifying the value of a const protection

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.