指针真正指向的是什么 [英] What a pointer really points to
问题描述
我找到了下面的示例代码,在此处描述的书中列出:
http://cartan.cas.suffolk.edu/moin/OopDocbookWiki
结果有点令人惊讶。我猜它属于类别
的那是你得到的谎言。这种行为可以用标准来解释吗?是的,我知道确切的结果是未定义
行为。我可以看到发生了什么。但实际违规是什么?
//里程转换为公里。
#include< QTextStream>
QTextStream cin(stdin,QIODevice :: ReadOnly);
QTextStream cout(stdout,QIODevice :: WriteOnly);
QTextStream cerr(stderr,QIODevice :: WriteOnly );
const double m2k = 1.609; //转换常数
inline double mi2km(int miles){
return(miles * m2k);
}
int main(){
英里;
双公里;
cout<< 以英里为单位输入距离: <<冲洗;
cin>里程;
公里= mi2km(英里);
cout<< 这大约是
<< static_cast< int>(千米)
<< "公里"。<< endl;
cout<< 没有演员,千米="
<<公里<< endl;
double * dp = const_cast< double *>(& m2k);
cout<< m2k: << m2k<< endl;
cout<< & m2k:" << & m2k<< " dp: << dp<< endl;
cout<< * dp: << * dp<<结束;
* dp = 1.892; / *我们在这里尝试做什么?* /
cout<< 我们能否达成这一声明? " <<结束;
返回0;
}
/ * OUT
以英里为单位输入距离: 23
这是大约37公里。
没有演员,公里= 37.007
m2k:1.609
& m2k:0x8049048 dp:0x8049048
* dp:1.609
分段错误
* /
-
NOUN:1。遗嘱遗留给他人的金钱或财产。 2.从祖先或前任或过去那里下来的东西:b
宗教自由的遗产。 ETYMOLOGY:MidE legacie,副手办公室,来自OF,来自ML legatia的
,来自L legare,以及deute,遗赠。 www.bartleby.com/61/
Steven T. Hatton写道:
我找到了下面的示例代码,列在这里描述的书中:
http://cartan.cas.suffolk.edu/moin/OopDocbookWiki
嗯,这只是表明你不是它;-)
#include< quan / out / length.hpp>
int main(){
std :: cout<< 以英里为单位输入距离:;
quan :: length :: mi miles;
std :: cin> miles.reference_numeric_value< quan :: length: :mi>(); ;
quan :: length :: km千里=英里;
std :: cout<< 大约是 <<公里<< " \ n \ nn";
std :: cout<< 我们可以达到这个声明吗?\ n \ n" ;;
std :: cout<< "当然! ......感受全权的力量:-) \ n \ n" ;;
std :: cout<< " ------------------------------------- \\\
";
std :: cout<< Quan文档:\ n \ nn;
std :: cout<< "< http://quan.sourceforge.net> \ n \ n";
std :: cout<< Quan download and cvs:\ n\ n;
std :: cout<< "< http://sourceforge.net/projects/quan> \ n \ n";
std :: cout<< " ------------------------------------- \\\
";
返回0;
}
/ *
输出:
以英里为单位输入距离:2
大约是3.21869公里
我们可以达到这个声明吗?
当然可以! ......感受全权的力量:-)
-------------------------- -----------
Quan文档:
< http://quan.sourceforge.net>
全下载和简历:
< http://sourceforge.net/projects/quan>
-------------------------------------
* /
" Steven T. Hatton" < ch ******** @ germania.supwrote in message
news:c _ ********************** ********@speakeasy.ne t
我找到了下面的示例代码,在此处描述的书中列出:
http://cartan.cas.suffolk.edu/moin/OopDocbookWiki
结果有点令人惊讶。我猜它属于类别
的那是你得到的谎言。这种行为可以用标准来解释吗?是的,我知道确切的结果是未定义
行为。我可以看到发生了什么。但实际的
违规是什么?
//里程转换为公里。
#include< QTextStream>
QTextStream cin(stdin,QIODevice :: ReadOnly);
QTextStream cout(stdout,QIODevice :: WriteOnly);
QTextStream cerr(stderr,QIODevice :: WriteOnly);
const double m2k = 1.609; //转换常数
inline double mi2km(int miles){
return(miles * m2k);
}
int main(){
英里;
双公里;
cout<< 以英里为单位输入距离: <<冲洗;
cin>里程;
公里= mi2km(英里);
cout<< 这大约是
<< static_cast< int>(千米)
<< "公里"。<< endl;
cout<< 没有演员,千米="
<<公里<< endl;
double * dp = const_cast< double *>(& m2k);
cout<< m2k: << m2k<< endl;
cout<< & m2k:" << & m2k<< " dp: << dp<< endl;
cout<< * dp: << * dp<<结束;
* dp = 1.892; / *我们在这里尝试做什么?* /
cout<< 我们能否达成这一声明? " <<结束;
返回0;
}
/ * OUT
以英里为单位输入距离: 23
这是大约37公里。
没有演员,公里= 37.007
m2k:1.609
& m2k:0x8049048 dp:0x8049048
* dp:1.609
分段错误
* /
删除不相关的,你有:
const double m2k = 1.609;
int main()
{
double * dp = const_cast< double *>(& m2k);
* dp = 1.892;
返回0;
}
因此dp指向一个const变量,你尝试使用解除引用的dp更改const
变量。
操作系统大概将m2k存储在
内存的只读部分,因此当你尝试写入时会引发分段错误
记忆。
-
John Carson
" Steven T. Hatton" < ch ******** @ germania.supwrote in message
news:c _ ********************** ********@speakeasy.ne t
>我找到了下面的示例代码,在此处描述的书中列出:
< a rel =nofollowhref =http://cartan.cas.suffolk.edu/moin/OopDocbookWikitarget =_ blank> http://cartan.cas.suffolk.edu/moin/OopDocbookWiki
结果有点令人惊讶。我想它属于那就是你说谎的类别。这种行为可以在标准中解释吗?是的,我知道确切的结果是未定义的行为。我可以看到发生了什么。但是实际违反了什么?
//里程转换为公里。
#include< QTextStream>
QTextStream cin(stdin, QIODevice :: ReadOnly);
QTextStream cout(stdout,QIODevice :: WriteOnly);
QTextStream cerr(stderr,QIODevice :: WriteOnly);
const double m2k = 1.609; //转换常数
内联双mi2km(英里){
返回(英里* m2k);
}
int main(){
英里;
双公里;
cout<< 以英里为单位输入距离: <<冲洗;
cin>英里;
公里= mi2km(英里);
cout<< 这大约是
<< static_cast< int>(千米)
<< "公里"。<< endl;
cout<< 没有演员阵容,千米="
<<公里<< endl;
double * dp = const_cast< double *>(& m2k);
cout<< m2k: << m2k<< endl;
cout<< & m2k:" << & m2k<< " dp: << dp<< endl;
cout<< * dp: << * dp<<结束;
* dp = 1.892; / *我们在这里尝试做什么?* /
cout<< 我们能否达成这一声明? " << endl;
返回0;
}
/ / OUT
以英里为单位输入距离:23
这大约是37km。
没有演员,公里= 37.007
m2k:1.609
& m2k:0x8049048 dp:0x8049048
* dp:1.609
分段错误
* /
删除不相关的,你有:
拥有实际编译和运行的东西通常很好。 />
使用标准库编译代码需要花费很少的金额。
$ blockquote class =post_quotes>
const double m2k = 1.609 ;
int main()
{
double * dp = const_cast< double *>(& m2k);
* dp = 1.892;
返回0;
}
因此dp指向a const变量
标准指定它应该指向什么,或者指定了什么?
和你看使用解除引用的dp更改const
变量。
操作系统可能将m2k存储在
$的只读部分中b $ b内存,因此当您尝试写入该内存时会引发分段错误。
当我满足时,我明白发生了什么。我只是不确定违反了什么规则
。我想我会回过头来通过标准的正式奥秘的巧妙散文。
-
NOUN:1。遗嘱遗留给他人的金钱或财产。 2.从祖先或前任或过去那里下来的东西:b
宗教自由的遗产。 ETYMOLOGY:MidE legacie,副手办公室,来自OF,来自ML legatia的
,来自L legare,以及deute,遗赠。 www.bartleby.com/61/
I found the example code below, listed in the book described here:
http://cartan.cas.suffolk.edu/moin/OopDocbookWiki
The result was a bit surprising. I guess it falls into the category
of "that''s what you get for lying". Can the behavior demonstrated be
explained in standardese? Yes, I know the exact result is "undefined
behavior". I can see what happened. But what was the actual violation?
// Miles are converted to kilometers.
#include <QTextStream>
QTextStream cin(stdin, QIODevice::ReadOnly);
QTextStream cout(stdout, QIODevice::WriteOnly);
QTextStream cerr(stderr, QIODevice::WriteOnly);
const double m2k = 1.609; // conversion constant
inline double mi2km(int miles) {
return (miles * m2k);
}
int main() {
int miles;
double kilometers;
cout << "Enter distance in miles: " << flush;
cin >miles ;
kilometers = mi2km(miles);
cout << "This is approximately "
<< static_cast<int>(kilometers)
<< "km."<< endl;
cout << "Without the cast, kilometers = "
<< kilometers << endl;
double* dp = const_cast<double*>(&m2k);
cout << "m2k: " << m2k << endl;
cout << "&m2k: " << &m2k << " dp: " << dp << endl;
cout << "*dp: " << *dp << endl;
*dp = 1.892; /* What are we attempting to do here?*/
cout << "Can we reach this statement? " << endl;
return 0;
}
/*OUT
Enter distance in miles: 23
This is approximately 37km.
Without the cast, kilometers = 37.007
m2k: 1.609
&m2k: 0x8049048 dp: 0x8049048
*dp: 1.609
Segmentation fault
*/
--
NOUN:1. Money or property bequeathed to another by will. 2. Something handed
down from an ancestor or a predecessor or from the past: a legacy of
religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
Steven T. Hatton wrote:I found the example code below, listed in the book described here:
http://cartan.cas.suffolk.edu/moin/OopDocbookWikiWell that just goes to show you don''t it ;-)
#include <quan/out/length.hpp>
int main() {
std::cout << "Enter distance in miles: ";
quan::length::mi miles;
std::cin >miles.reference_numeric_value<quan::length::mi>() ; ;
quan::length::km kilometers = miles;
std::cout << "\nThat is approximately " << kilometers << "\n\n";
std::cout << "Can we reach this statement?\n\n";
std::cout << "Sure! ... Feel the Power of The Quan :-)\n\n";
std::cout << "-------------------------------------\n";
std::cout << "Quan documentation :\n\n";
std::cout << "<http://quan.sourceforge.net>\n\n";
std::cout << "Quan download and cvs :\n\n";
std::cout << "<http://sourceforge.net/projects/quan>\n\n";
std::cout << "-------------------------------------\n";
return 0;
}
/*
output:
Enter distance in miles: 2
That is approximately 3.21869 km
Can we reach this statement?
Sure! ... Feel the Power of The Quan :-)
-------------------------------------
Quan documentation :
<http://quan.sourceforge.net>
Quan download and cvs :
<http://sourceforge.net/projects/quan>
-------------------------------------
*/
"Steven T. Hatton" <ch********@germania.supwrote in message
news:c_******************************@speakeasy.ne tI found the example code below, listed in the book described here:
http://cartan.cas.suffolk.edu/moin/OopDocbookWiki
The result was a bit surprising. I guess it falls into the category
of "that''s what you get for lying". Can the behavior demonstrated be
explained in standardese? Yes, I know the exact result is "undefined
behavior". I can see what happened. But what was the actual
violation?
// Miles are converted to kilometers.
#include <QTextStream>
QTextStream cin(stdin, QIODevice::ReadOnly);
QTextStream cout(stdout, QIODevice::WriteOnly);
QTextStream cerr(stderr, QIODevice::WriteOnly);
const double m2k = 1.609; // conversion constant
inline double mi2km(int miles) {
return (miles * m2k);
}
int main() {
int miles;
double kilometers;
cout << "Enter distance in miles: " << flush;
cin >miles ;
kilometers = mi2km(miles);
cout << "This is approximately "
<< static_cast<int>(kilometers)
<< "km."<< endl;
cout << "Without the cast, kilometers = "
<< kilometers << endl;
double* dp = const_cast<double*>(&m2k);
cout << "m2k: " << m2k << endl;
cout << "&m2k: " << &m2k << " dp: " << dp << endl;
cout << "*dp: " << *dp << endl;
*dp = 1.892; /* What are we attempting to do here?*/
cout << "Can we reach this statement? " << endl;
return 0;
}
/*OUT
Enter distance in miles: 23
This is approximately 37km.
Without the cast, kilometers = 37.007
m2k: 1.609
&m2k: 0x8049048 dp: 0x8049048
*dp: 1.609
Segmentation fault
*/Deleting the irrelevant, you have:
const double m2k = 1.609;
int main()
{
double* dp = const_cast<double*>(&m2k);
*dp = 1.892;
return 0;
}
Thus dp points to a const variable and you attempt to change that const
variable using a dereferenced dp.
The operating system has presumably stored m2k in a read-only section of
memory, so raises a segmentation fault when you attempt to write to that
memory.
--
John Carson
John Carson wrote:
"Steven T. Hatton" <ch********@germania.supwrote in message
news:c_******************************@speakeasy.ne t>I found the example code below, listed in the book described here:
http://cartan.cas.suffolk.edu/moin/OopDocbookWiki
The result was a bit surprising. I guess it falls into the category
of "that''s what you get for lying". Can the behavior demonstrated be
explained in standardese? Yes, I know the exact result is "undefined
behavior". I can see what happened. But what was the actual
violation?
// Miles are converted to kilometers.
#include <QTextStream>
QTextStream cin(stdin, QIODevice::ReadOnly);
QTextStream cout(stdout, QIODevice::WriteOnly);
QTextStream cerr(stderr, QIODevice::WriteOnly);
const double m2k = 1.609; // conversion constant
inline double mi2km(int miles) {
return (miles * m2k);
}
int main() {
int miles;
double kilometers;
cout << "Enter distance in miles: " << flush;
cin >miles ;
kilometers = mi2km(miles);
cout << "This is approximately "
<< static_cast<int>(kilometers)
<< "km."<< endl;
cout << "Without the cast, kilometers = "
<< kilometers << endl;
double* dp = const_cast<double*>(&m2k);
cout << "m2k: " << m2k << endl;
cout << "&m2k: " << &m2k << " dp: " << dp << endl;
cout << "*dp: " << *dp << endl;
*dp = 1.892; /* What are we attempting to do here?*/
cout << "Can we reach this statement? " << endl;
return 0;
}
/*OUT
Enter distance in miles: 23
This is approximately 37km.
Without the cast, kilometers = 37.007
m2k: 1.609
&m2k: 0x8049048 dp: 0x8049048
*dp: 1.609
Segmentation fault
*/
Deleting the irrelevant, you have:It''s often nice to have something that actually compiles and runs.
Compiling the code using the Standard Library would take a trivial amount
of effort.
const double m2k = 1.609;
int main()
{
double* dp = const_cast<double*>(&m2k);
*dp = 1.892;
return 0;
}
Thus dp points to a const variableWhat does the Standard specify it should point to, or is that specified?
and you attempt to change that const
variable using a dereferenced dp.
The operating system has presumably stored m2k in a read-only section of
memory, so raises a segmentation fault when you attempt to write to that
memory.As I sated, I understand what happened. I''m just not sure what rule was
violated. I guess I go back to slogging my way through the artful prose of
the formal arcana of the Standard.
--
NOUN:1. Money or property bequeathed to another by will. 2. Something handed
down from an ancestor or a predecessor or from the past: a legacy of
religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
这篇关于指针真正指向的是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!