请解释为什么这是未定义的行为 [英] please explain why this is Undefined Behavior
问题描述
有人比我更清楚,请向Ioannis Vranos解释为什么两个程序之后的
都表现出未定义的行为:
#include< iostream>
#include< cstring>
类SomeClass
{
公开:
双d;
int i;
浮动f;
long l;
};
int main()
{
使用命名空间std;
> unsigned char array [sizeof(SomeClass)];
SomeClass obj = {1,2,3,4};
memcpy(array,& obj,sizeof( obj)); SomeClass * p = reinterpret_cast< SomeClass *>(array);
cout<< p-> d<<<" "<<对 - I标记<<" "<<对 - > F<<" "<< p-> l<<" \ n";
}
#include< iostream>
#include < cstring>
类SomeClass
{
公开:
双d;
int i;
浮动f;
long l;
};
int main()
{
使用命名空间std;
unsigned char array [ sizeof(SomeClass)];
SomeClass obj = {1,2,3,4};
SomeClass * p = new(array)SomeClass(obj);
cout<< p-> d<<" "<<对 - I标记<<" "<<对 - > F<<" "<< p-> l<<" \ n" ;;
}
REH写道:有人比我更清楚,请向Ioannis Vranos解释为什么
以下两个程序都表现出不明确的行为:
到目前为止你管理了什么?
#include< iostream>
#include< cstring>
类SomeClass
{
公开:
双d;
int i;
浮动f;
长l;
};
int main()
{
使用命名空间std;
unsigned char array [sizeof(SomeClass)];
SomeClass obj = {1,2,3,4};
memcpy(数组,& obj,sizeof(obj));
SomeClass * p = reinterpret_cast< SomeClass *>(array);
正如我所看到的,''obj''和''array''有不同的对齐要求。
重新解释''数组''到''obj''可能违反从多字节对象加载值的硬件协议。
cout<< p-> d<<<" "<<对 - I标记<<" "<<对 - > F<<" "<<< p-> l<<" \ n" ;;
}
#include< iostream>
#include< cstring>
类SomeClass
{
公开:
double d;
int i;
float f;
long l;
};
int main()
{
使用命名空间std;
unsigned char数组[sizeof(SomeClass)];
SomeClass obj = {1,2,3,4};
SomeClass * p = new(array)SomeClass(obj);
这里也是一样。
cout<< p-> d<<<" "<<对 - I标记<<" "<<对 - > F<<" "<< p-> l<<" \ n" ;;
}
您可以解决这个问题如果你去了
unsigned char * array = new unsigned char [sizeof(SomeClass)];
然后原样。 ''new''返回正确对齐的指针。
V
REH写道:
有人比我更清楚,请向Ioannis Vranos解释为什么
以下两个程序都表现出不明确的行为:
#include< iostream>
#包括< cstring>
类SomeClass
{
公开:
双d;
int i;
float f ;
长l;
};
我不确定Plain Ole Data
Structure定义的更多毛茸茸的结尾,但我觉得安全地称那个。
int main()
{
使用命名空间std;
unsigned char数组[sizeof(SomeClass)];
SomeClass obj = {1,2,3,4};
memcpy(数组,& obj,sizeof(obj));
SomeClass * p = reinterpret_cast< SomeClass *>(数组);
定义明确。 reinterpret_cast用于存储,其内容你知道为b
是正确的类型。
cout<< p-> d<<<" "<<对 - I标记<<" "<<对 - > F<<" "<< p-> l<<" \ n" ;;
}
int main()
{
使用命名空间std;
unsigned char数组[sizeof(SomeClass)];
SomeClass obj = {1,2,3,4};
SomeClass * p = new(阵列)SomeClass的(OBJ);
定义明确。 obj将复制构造成具有有效类型
和有效存储的新对象。
cout<< p-> d<<<" "<<对 - I标记<<" "<<对 - > F<<" "<<对 - →1<<" \\\
英寸;
在这里,我们需要匹配〜删除。对?或者POD是否需要
?无论如何我会添加它然后让编译器把它扔掉。
}
-
Phlip
http://industrialxp.org/community /bi...UserInterfaces
" Victor Bazarov" <五******** @ comAcast.net>在消息中写道
news:wi ******************* @ newsread1.mlpsca01.us.t o.verio.net ... < blockquote class =post_quotes> REH写道:有人比我更清楚,请向Ioannis Vranos解释为什么
以下两个程序都表现出不明确的行为:
你管理的是什么到目前为止?
你不能随意将char *转换为任何其他指针,因为
对齐问题。但是,我是对的不是吗?这是未定义的行为。
#include< iostream>
#include< cstring>
类SomeClass
{
公开:
双d;
int i;
浮动f;
long l;
};
int main()
{
使用命名空间std;
unsigned char数组[sizeof(SomeClass)];
SomeClass obj = {1,2,3,4};
memcpy(数组,& obj,sizeof(obj));
SomeClass * p = reinterpret_cast< SomeClass *>(数组);
正如我所看到的,''obj''和''array''具有不同的对齐要求。
将''array''重新解释为''obj' '可能违反了从多字节对象加载值的硬件协议。
cout<< p> d<<<" "<<对 - I标记<<" "<<对 - > F<<" "<<< p-> l<<" \ n" ;;
}
#include< iostream>
#include< cstring>
类SomeClass
{
公开:
double d;
int i;
float f;
long l;
};
int main()
{
使用命名空间std;
unsigned char数组[sizeof(SomeClass)];
SomeClass obj = {1,2,3,4};
SomeClass * p = new(array)SomeClass(obj);
同样的事情。
cout<< p-> d<<<" "<<对 - I标记<<" "<<对 - > F<<" "<<< p-> l<<" \ nn" ;;
}
如果你去了,你可以解决这个问题br />
unsigned char * array = new unsigned char [sizeof(SomeClass)];
然后就是这样。 ''new''返回正确对齐的指针。
V
Someone more articulate than me please explain to Ioannis Vranos why the
following two programs both exhibit undefined behavior:
#include <iostream>
#include <cstring>
class SomeClass
{
public:
double d;
int i;
float f;
long l;
};
int main()
{
using namespace std;
unsigned char array[sizeof(SomeClass)];
SomeClass obj= {1, 2, 3, 4};
memcpy(array, &obj, sizeof(obj));
SomeClass *p= reinterpret_cast<SomeClass *>(array);
cout<<p->d<<" "<<p->i<<" "<<p->f<<" "<<p->l<<"\n";
}
#include <iostream>
#include <cstring>
class SomeClass
{
public:
double d;
int i;
float f;
long l;
};
int main()
{
using namespace std;
unsigned char array[sizeof(SomeClass)];
SomeClass obj= {1, 2, 3, 4};
SomeClass *p= new(array)SomeClass(obj);
cout<<p->d<<" "<<p->i<<" "<<p->f<<" "<<p->l<<"\n";
}
REH wrote:Someone more articulate than me please explain to Ioannis Vranos why the
following two programs both exhibit undefined behavior:
What have you managed so far?
#include <iostream>
#include <cstring>
class SomeClass
{
public:
double d;
int i;
float f;
long l;
};
int main()
{
using namespace std;
unsigned char array[sizeof(SomeClass)];
SomeClass obj= {1, 2, 3, 4};
memcpy(array, &obj, sizeof(obj));
SomeClass *p= reinterpret_cast<SomeClass *>(array);
As I can see, ''obj'' and ''array'' have different alignment requirements.
Reinterpret-casting ''array'' to ''obj'' may violate hardware protocols for
loading values from multi-byte objects.
cout<<p->d<<" "<<p->i<<" "<<p->f<<" "<<p->l<<"\n";
}
#include <iostream>
#include <cstring>
class SomeClass
{
public:
double d;
int i;
float f;
long l;
};
int main()
{
using namespace std;
unsigned char array[sizeof(SomeClass)];
SomeClass obj= {1, 2, 3, 4};
SomeClass *p= new(array)SomeClass(obj);
Same thing here.
cout<<p->d<<" "<<p->i<<" "<<p->f<<" "<<p->l<<"\n";
}
You could fix that if you went
unsigned char *array = new unsigned char[sizeof(SomeClass)];
and then as it was. ''new'' returns pointers properly aligned.
V
REH wrote:
Someone more articulate than me please explain to Ioannis Vranos why the
following two programs both exhibit undefined behavior:#include <iostream>
#include <cstring>
class SomeClass
{
public:
double d;
int i;
float f;
long l;
};
I''m unsure of the more hairy end of the definition of "Plain Ole Data
Structure", but I feel safe calling that one.
int main()
{
using namespace std;
unsigned char array[sizeof(SomeClass)];
SomeClass obj= {1, 2, 3, 4};
memcpy(array, &obj, sizeof(obj));
SomeClass *p= reinterpret_cast<SomeClass *>(array);
Well-defined. reinterpret_cast is used on storage whose contents you know to
be the correct type.
cout<<p->d<<" "<<p->i<<" "<<p->f<<" "<<p->l<<"\n";
} int main()
{
using namespace std;
unsigned char array[sizeof(SomeClass)];
SomeClass obj= {1, 2, 3, 4};
SomeClass *p= new(array)SomeClass(obj);
Well-defined. obj got copy-constructed into a new object with a valid type
and valid storage.
cout<<p->d<<" "<<p->i<<" "<<p->f<<" "<<p->l<<"\n";
Down here, we need a matching ~delete. Right? Or does the POD make it not
needed? I would add it anyway and let the compiler throw it away.
}
--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:wi*******************@newsread1.mlpsca01.us.t o.verio.net...REH wrote:Someone more articulate than me please explain to Ioannis Vranos why the
following two programs both exhibit undefined behavior:
What have you managed so far?
That you cannot arbitrarily cast a char* to any other pointer, because of
alignment issues. But, I am right aren''t I? This is undefined behavior.
#include <iostream>
#include <cstring>
class SomeClass
{
public:
double d;
int i;
float f;
long l;
};
int main()
{
using namespace std;
unsigned char array[sizeof(SomeClass)];
SomeClass obj= {1, 2, 3, 4};
memcpy(array, &obj, sizeof(obj));
SomeClass *p= reinterpret_cast<SomeClass *>(array);
As I can see, ''obj'' and ''array'' have different alignment requirements.
Reinterpret-casting ''array'' to ''obj'' may violate hardware protocols for
loading values from multi-byte objects.
cout<<p->d<<" "<<p->i<<" "<<p->f<<" "<<p->l<<"\n";
}
#include <iostream>
#include <cstring>
class SomeClass
{
public:
double d;
int i;
float f;
long l;
};
int main()
{
using namespace std;
unsigned char array[sizeof(SomeClass)];
SomeClass obj= {1, 2, 3, 4};
SomeClass *p= new(array)SomeClass(obj);
Same thing here.
cout<<p->d<<" "<<p->i<<" "<<p->f<<" "<<p->l<<"\n";
}
You could fix that if you went
unsigned char *array = new unsigned char[sizeof(SomeClass)];
and then as it was. ''new'' returns pointers properly aligned.
V
这篇关于请解释为什么这是未定义的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!