【发布时间】:2010-12-23 12:59:47
【问题描述】:
下面的例子可以编译,但是输出很奇怪:
#include <iostream>
#include <cstring>
struct A
{
int a;
char b;
bool c;
};
int main()
{
A v;
std::memset( &v, 0xff, sizeof(v) );
std::cout << std::boolalpha << ( true == v.c ) << std::endl;
std::cout << std::boolalpha << ( false == v.c ) << std::endl;
}
输出是:
true
true
谁能解释一下原因?
如果重要的话,我使用的是 g++ 4.3.0
【问题讨论】:
-
不要那样使用 memset。它完全忽略了您正在写入的类型。
标签: c++