HOWTO:C和C ++中的const和指针变体 [英] HOWTO: const and pointer variants in C and C++
问题描述
HOWTO:C和C ++中的const和指针变体:
void test()
{
int n = 1; // n是int类型的非const数据
++ n; // ok
const int c = 2; // c是int类型的const数据
// ++ c; //错误
//案例1:
int * p1 =& n; // p1是非常数ptr到非常数数据
++ p1; // ok
* p1 + = 3; // ok
// int * px =& c; //错误
//案例2:
int * const p2 =& n; // p2是非常数数据的常量p />
// ++ p2; //错误
* p2 + = 3; // ok
//案例3:
const int * p3 =& c; // p3是const数据的非常量ptr
++ p3; // ok
// * p3 + = 3; //错误
//案例4:
const int * const p4 =& c; // p4是const数据的const ptr
// ++ p4; //错误
// * p4 + = 3; //错误
//案例5:
const int * const p5 =& n; //类似于Case4,但是当通过p5访问时,非const n被视为const数据
// ++ p5; //错误
// * p5 + = 3; //错误
//案例6:
const int * const p6 =& c; //与Case4相同
// ++ p6; //错误
// * p6 + = 3; //错误
//案例7:
const int * const p7 =& n; //与Case5相同
// ++ p7; //错误
// * p7 + = 3; //错误
// const * int p8 =& n; //错误
// const * int p9 =& c; //错误
// const int const * pa =& n; //错误:复制''const''
// const int const * pb =& c; //错误:复制''const''
}
为了安全和速度,你应该尽可能使用const。
HOWTO: const and pointer variants in C and C++ :
void test()
{
int n = 1; // n is non-const data of type int
++n; // ok
const int c = 2; // c is const data of type int
// ++c; // err
// Case1:
int* p1 = &n; // p1 is a non-const ptr to non-const data
++p1; // ok
*p1 += 3; // ok
// int* px = &c; // err
// Case2:
int* const p2 = &n; // p2 is a const ptr to non-const data
// ++p2; // err
*p2 += 3; // ok
// Case3:
const int* p3 = &c; // p3 is a non-const ptr to const data
++p3; // ok
// *p3 += 3; // err
// Case4:
const int* const p4 = &c; // p4 is a const ptr to const data
// ++p4; // err
// *p4 += 3; // err
// Case5:
const int* const p5 = &n; // Similar to Case4 but here non-const n is treated as const data when accessed via p5
// ++p5; // err
// *p5 += 3; // err
// Case6:
const int *const p6 = &c; // same as Case4
// ++p6; // err
// *p6 += 3; // err
// Case7:
const int *const p7 = &n; // Same as Case5
// ++p7; // err
// *p7 += 3; // err
// const* int p8 = &n; // err
// const* int p9 = &c; // err
// const int const* pa = &n; // err: duplicate ''const''
// const int const* pb = &c; // err: duplicate ''const''
}
For safety and speed you should use const whenever possible.
推荐答案
Adem写道:
Adem wrote:
HOWTO:C和C ++中的const和指针变体:
无效测试()
{
int n = 1; // n是int类型的非const数据
++ n; // ok
const int c = 2; // c是int类型的const数据
// ++ c; // err
HOWTO: const and pointer variants in C and C++ :
void test()
{
int n = 1; // n is non-const data of type int
++n; // ok
const int c = 2; // c is const data of type int
// ++c; // err
我讨厌迂腐,但这行不是错误,因为它被注释掉了。
I hate to be pedantic, but this line isn''t an error since it''s commented out.
//案例1:
int * p1 =& n; // p1是非常数ptr到非常数数据
++ p1; // ok
* p1 + = 3; // ok
// Case1:
int* p1 = &n; // p1 is a non-const ptr to non-const data
++p1; // ok
*p1 += 3; // ok
不,这是未定义的行为,因为你正在取消引用一个过去的结尾
指针。但这不是:
int * p1 =& n;
++ p1;
- p1;
* p1 + = 3;
[...]
No, that''s undefined behavior because you''re dereferencing a past-the-end
pointer. This isn''t, though:
int* p1 = &n;
++p1;
--p1;
*p1 += 3;
[...]
For安全和速度你应该尽可能使用const。
For safety and speed you should use const whenever possible.
注意显示const帮助速度的任何示例?也就是说,一个
程序,删除const不会改变程序的语义,但是会降低它的速度。我不反对使用const,只是询问这个经常提出的声明。
Care to show any examples of where const helps speed-wise? That is, a
program where removing const wouldn''t change the semantics of the program,
but would reduce its speed. I''m not arguing against using const, just
questioning this often-made claim.
blargg写道:
blargg wrote:
注意显示const帮助速度的任何示例?也就是说,一个
程序,删除const不会改变程序的语义,但是会降低它的速度。我不反对使用const,只是询问这个经常提出的声明。
Care to show any examples of where const helps speed-wise? That is, a
program where removing const wouldn''t change the semantics of the program,
but would reduce its speed. I''m not arguing against using const, just
questioning this often-made claim.
const可以帮助编译器确定变量
是否可以用机器码中的常量替换。
带操作数的操作码,通常有更小的更快版本
版本操作常量。
一般原则,
我更喜欢给编译器我能提供的任何信息。
我不认为删除const
会改变程序的语义。
假设那个,你的意思是从正确的程序开始
碰巧使用const关键字,然后删除关键字。
-
pete
const could help a compiler determine if a variable
could be replaced with a constant in machine code.
Opcodes with operands, usually have smaller faster
versions which operate on constants.
On general principle,
I prefer to give the compiler whatever information I can.
I don''t think that removing const
ever changes the semantics of a program.
Assuming that by that, you mean to start with a correct program
which happens to use the const keyword, and then remove the keyword.
--
pete
pf **** *@mindspring.com 写道:
blargg写道:
blargg wrote:
小心显示任何const有助于速度提升的例子?也就是说,一个
程序,删除const不会改变程序的语义,但是会降低它的速度。我不反对使用const,只是询问这个经常提出的声明。
Care to show any examples of where const helps speed-wise? That is, a
program where removing const wouldn''t change the semantics of the program,
but would reduce its speed. I''m not arguing against using const, just
questioning this often-made claim.
const可以帮助编译器确定变量
是否可以用机器码中的常量替换。
带有操作数的操作码,通常具有更小的更快的版本,它们运行在常量上。
const could help a compiler determine if a variable
could be replaced with a constant in machine code.
Opcodes with operands, usually have smaller faster
versions which operate on constants.
[...]
是的,但是你能提供const实际帮助的例子
编译器确定这个,在没有const的情况下它无法确定它?
关于唯一的一个是文件范围内的非静态变量,其中
编译器通常不能在其他翻译中的代码之后假设它的值
单位已经执行,除非它被声明为const。变体是一个局部const
对象,其地址被传递给一个函数;删除const会阻止编译器假设其值在函数
调用期间没有改变。我认为这些情况很少见。
[...]
Yes, but can you provide EXAMPLES of where const actually helps the
compiler determine this, where it couldn''t determine it without const?
About the only one is a non-static variable at file scope, where the
compiler can''t in general assume its value after code in other translation
units has executed, unless it''s declared const. A variantis a local const
object whose address is passed to a function; removing const would prevent
the compiler from assuming its value doesn''t change during the function
call. I contend that these are rare cases.
这篇关于HOWTO:C和C ++中的const和指针变体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!