Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't assign const initialize while compile by pointer

Tags:

c++

I'm curious about why my research result is strange

#include <iostream>

int test()
{
    return 0;
}

int main()
{
    /*include either the next line or the one after*/
    const int a = test(); //the result is 1:1
    const int a = 0; //the result is 0:1

    int* ra = (int*)((void*)&a);
    *ra = 1;
    std::cout << a << ":" << *ra << std::endl;
    return 0;
}

why the constant var initialize while runtime can completely change, but initialize while compile will only changes pointer's var?

like image 728
Alan Jian Avatar asked Jun 25 '20 06:06

Alan Jian


People also ask

Is it possible to assign a constant to a pointer variable?

We can create a pointer to a constant in C, which means that the pointer would point to a constant variable (created using const). We can also create a constant pointer to a constant in C, which means that neither the value of the pointer nor the value of the variable pointed to by the pointer would change.

Which constant value can be assigned to a pointer variable?

Pointer to constant As the name itself indicates, the value of the variable to which the pointer is pointing, is constant. In other words, a pointer through which one cannot change the value of the variable to which it points is known as a pointer to constant.

What is the use of const pointer?

A pointer to a const value (sometimes called a pointer to const for short) is a (non-const) pointer that points to a constant value. In the above example, ptr points to a const int . Because the data type being pointed to is const, the value being pointed to can't be changed. We can also make a pointer itself constant.

Does const improve performance?

const correctness can't improve performance because const_cast and mutable are in the language, and allow code to conformingly break the rules. This gets even worse in C++11, where your const data may e.g. be a pointer to a std::atomic , meaning the compiler has to respect changes made by other threads.


1 Answers

The function isn't really that relevant here. In principle you could get same output (0:1) for this code:

int main() {
    const int a = 0;
    int* ra = (int*)((void*)&a);
    *ra = 1;
    std::cout << a << ":" << *ra;
}

a is a const int not an int. You can do all sorts of senseless c-casts, but modifiying a const object invokes undefined behavior.

By the way in the above example even for std::cout << a << ":" << a; the compiler would be allowed to emit code that prints 1:0 (or 42:3.1415927). When your code has undefinded behavior anything can happen.

PS: the function and the different outcomes is relevant if you want to study internals of your compiler and what it does to code that is not valid c++ code. For that you best look at the output of the compiler and how it differs in the two cases (https://godbolt.org/).

like image 72
463035818_is_not_a_number Avatar answered Oct 22 '22 20:10

463035818_is_not_a_number