#include <iostream>
using namespace std;
int main() {
const int N = 22;
int * pN = const_cast<int*>(&N);
*pN = 33;
cout << N << '\t' << &N << endl;
cout << *pN << '\t' << pN << endl;
}
Run Code Online (Sandbox Code Playgroud)
22 0x22ff74
33 0x22ff74
为什么同一地址有两个不同的值?
易失性写入易失性const是否会引入未定义的行为?如果我在写作时放弃挥发怎么办?
volatile const int x = 42;
const volatile int *p = &x;
*(volatile int *)p = 8; // Does this line introduce undefined behavior?
*(int *)p = 16; // And what about this one?
Run Code Online (Sandbox Code Playgroud)
我试图理解c ++中的const.我在下面的代码片段中写了这个:
const int x=5;
int *ptr;
ptr=(int*)&x;
cout<<"address of x="<<&x<<endl;
cout<<"value of ptr="<<ptr<<endl;
*ptr=11;
cout<<"*ptr="<<*ptr<<endl;
cout<<"x="<<x;
Run Code Online (Sandbox Code Playgroud)
输出是
address of x=0x28fef8
address of ptr=0x28fef8
*ptr=11
x=5
Run Code Online (Sandbox Code Playgroud)
由于ptr指向x,我确信*ptr和x的值是相同的.为什么价值不同?我知道x是const,但是,我通过执行*ptr来改变内存地址的值.请告诉我我错过了什么.
&i(内部主函数)和p(内部函数函数)都保持相同的地址.我知道一个常量对象/变量不能被修改但我能够在func函数中使用(*p)++增加变量i,但结果并没有反映在main函数中.这是为什么?
#include <iostream>
using namespace std;
void func(int *p){
(*p)++;
printf("%p %d\n", p, *p);
}
int main(int argc, char **argv){
const int i = 47;
const int *p = &i;
func(const_cast<int *>(p));
printf("%p %d\n", &i, i);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到这个输出:
000000000022fe44 48
000000000022fe44 47
Run Code Online (Sandbox Code Playgroud) 我正在通过GDB对用C++编写的项目进行调试,并发现在GNU C++编译器中没有警告或错误地修改了const.
这不是我正在调试的程序,但这是我目睹的行为的一个例子:
#include <iostream>
int main(int argc, char *argv[]) {
const int x = 10;
int *px = (int *)&x;
++*px;
std::cout << "*px: " << *px << "\n";
std::cout << "x: " << x << "\n";
for (int i = 0; i < x; ++i)
std::cout << i+1 << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我不能代表其他编译器,因为我只使用GNU C++编译器4.9.2版进行了测试.为什么允许这样的事情?这打破了const对象的整个点.
我用g ++ main.c -Wall -Werror编译了上面的代码
输出:
*px: 11
x: 10
1
2
3
4
5
6
7
8
9
10
Run Code Online (Sandbox Code Playgroud) #include <iostream>
using namespace std;
int main()
{
const int kiNum = 100;
int* ptr = const_cast<int*>(&kiNum);
*ptr = 200;
cout<<"kiNum: "<<kiNum; // The value still prints 100 on the console??
return 0;
}
output:
kiNum = 100
Run Code Online (Sandbox Code Playgroud)
在上面的代码片段中,我试图在const_cast之后更改const整数的值,然后更改地址处的值,但控制台仍然打印旧值(我使用的是visual studio 2012)
前几天,一位C++培训师告诉我,"const"只在编译时(静态)有意义,因此在运行时没有影响......但是当我测试这个例子时:
const int x = 5;
int * px = const_cast<int*>(&x);
*px = 10;
std::cout << "x = " << x <<std::endl; // x = 5 ???
Run Code Online (Sandbox Code Playgroud)
x不是用10修改的!然而,如果我们使用指针,这个例子按预期工作:
const int * x = new int(5);
int * px = const_cast<int*>(x);
*px = 10;
std::cout << "x = " << *x <<std::endl; // x = 10
Run Code Online (Sandbox Code Playgroud)
那么,这个C++教练是错的?
我试图通过其地址更改const变量的值.
遵循以下代码:
#include <iostream>
#include <string>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#include <stdio.h>
using namespace std;
int main(void)
{
uint64_t const x = -1;
uint64_t *b = reinterpret_cast<uint64_t*>(0x28ff10);
cout<< x << endl;
cout<< &x << " " << b << " " << *b << endl;
printf("%p\n", &x);
*b = 10;
cout<< &x << " " << x << " " << b << " " << *b << " " << *(reinterpret_cast<uint64_t*>(0x28ff10)) <<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
使用MinGW编译4.8.1 …