为什么C ++使用memset(addr,0,sizeof(T))构造一个对象?标准还是编译器错误? [英] why c++ use memset(addr,0,sizeof(T)) to construct a object? Standard or compiler bug?
问题描述
这个问题与我的另一篇文章有关:为什么allocate_shared和make_shared太慢了
在这里,我可以更清楚地描述这个问题。
考虑一下以下代码:
结构A {
char data_ [0x10000];
};
Class C {
public:
C():a_(){}
A a_;
};
int main(){
C c;
返回0;
}
我为代码 C()找到:a_ ()
,则编译器使用 memset(addr,0,0x10000)
作为A的构造函数。并且如果类型A具有空的构造函数,则asm代码正确。
为了更清楚地描述问题,我编写了一些测试代码:
#include< stdlib.h>
struct A {
// A(){}
char data_ [0x10000];
void dummy(){//避免由编译器优化擦除
data_ [rand()%sizeof(data_)] = 1;
}
int dummy2(){//避免编译器优化擦除
return data_ [0];
}
};
B类{
public:
template< class ... T> B(T& ... t)
:a_(std :: forward< T>(t)...){
}
A a_;
};
C类{
public:
C():a_(){
}
A a_;
};
template< class ... T>
int test(T& ... t){
A a(t ...);
a.dummy();
return a.dummy2();
}
int main(){
A a;
a.dummy();
自动r1 = a.dummy2();
auto r2 = test();
B b;
b.a_.dummy();
自动r3 = b.a_.dummy2();
C c;
c.a_.dummy();
自动r4 = c.a_.dummy2();
返回r1 + r2 + r3 + r4;
}
我在Windows 10,x86发行版中使用vs2017编译了代码。
然后我检查了asm代码:
template< class ... T>
int test(T& ... t){
00E510B8 call _chkstk(0E51CE0h)
00E510BD mov eax,dword ptr [__security_cookie(0E53004h)]
00E510C2 xor eax,ebp
00E510C4 mov dword ptr [ebp-4],eax
A a(t ...);
00E510C7 push 10000h
00E510CC lea eax,[a]
00E510D2 push 0
00E510D4 push eax
00E510D5 call _memset(0E51C3Ah)
00E510DA add esp, 0Ch
a.dummy();
00E510DD调用dword ptr [__imp__rand(0E520B4h)]
}
00E510E3 mov ecx,dword ptr [ebp-4]
很明显,函数 test()
调用 memset(p,0,0x10000 )
。
如果我在A中添加一个空的构造函数(行 所以为什么在类型A没有构造函数的情况下代码调用memset,而在A有构造函数的情况下代码没有调用memset呢? ? 它是c ++标准的一部分,还是只是编译器错误? 显然是内存集(p ,0,sizeof(T))是无用且有害的,这会减慢程序速度。我该如何解决呢? 将被解析为用<初始化 A(){} $ c $ p>
。†当
A a(t ...);
a
code> t ... t ...
为空时,就像您打电话时一样它将被理解为值初始化 a
。
对于 A
没有用户提供的默认构造函数, value-initialize 将其所有成员清零,因此 memset
。
为 A
提供构造函数时,值初始化是调用默认的构造函数,您将其定义为不执行任何操作,因此将不会调用内存集
。
这不是编译器中的错误,这是必需的行为。要删除多余的内存集
,您只需编写 A a;
。在这种情况下, a
是默认初始化的,无论是否使用用户提供的构造函数,都不会自动清零。
†这很重要,因为 A a()
将被解析为称为 a $的函数c $ c>,返回类型为
A
This question is related to another post of mine: why allocate_shared and make_shared so slow
In here I can describe the question more clearly.
Think about the following code:
struct A {
char data_[0x10000];
};
class C {
public:
C() : a_() { }
A a_;
};
int main() {
C c;
return 0;
}
I found for the code C() : a_()
, the compiler uses memset(addr,0,0x10000)
as the constructor of the A. And if the type A has a empty constructor, the asm code is right.
To describe the issue more clearly, I wrote some test code:
#include <stdlib.h>
struct A {
//A() {}
char data_[0x10000];
void dummy() { // avoid optimize erase by compiler
data_[rand() % sizeof(data_)] = 1;
}
int dummy2() { // avoid optimize erase by compiler
return data_[0];
}
};
class B {
public:
template<class ... T> B(T&...t)
: a_(std::forward<T>(t)...) {
}
A a_;
};
class C {
public:
C() : a_() {
}
A a_;
};
template<class ... T>
int test(T&...t) {
A a(t...);
a.dummy();
return a.dummy2();
}
int main() {
A a;
a.dummy();
auto r1 = a.dummy2();
auto r2 = test();
B b;
b.a_.dummy();
auto r3 = b.a_.dummy2();
C c;
c.a_.dummy();
auto r4 = c.a_.dummy2();
return r1 + r2 + r3 + r4;
}
I compiled the code with vs2017, in windows 10, x86 release build. Then I checked the asm code:
template<class ... T>
int test(T&...t) {
00E510B8 call _chkstk (0E51CE0h)
00E510BD mov eax,dword ptr [__security_cookie (0E53004h)]
00E510C2 xor eax,ebp
00E510C4 mov dword ptr [ebp-4],eax
A a(t...);
00E510C7 push 10000h
00E510CC lea eax,[a]
00E510D2 push 0
00E510D4 push eax
00E510D5 call _memset (0E51C3Ah)
00E510DA add esp,0Ch
a.dummy();
00E510DD call dword ptr [__imp__rand (0E520B4h)]
}
00E510E3 mov ecx,dword ptr [ebp-4]
It is very clear that the function test()
calls memset(p, 0, 0x10000)
.
And if I add an empty constructor in A (line A(){}
), the compiler removes the memset.
So why does the code call memset when type A does not have constructor but does not call memset when A has a constructor?
Is it part of the c++ standard, or just a compiler bug?
Obviously the memset(p, 0, sizeof(T)) is useless and harmful which slows down the program. How do I workaround it?
A a(t...);
Will be parsed as initializing a
with t...
.† When t...
is empty, as when you call it, this will be understood as value-initializing a
.
For A
without a user-provided default constructor, value-initialize is to zero all its members, hence the memset
.
When you provide a constructor for A
, value-initialize is to call the default constructor, which you defined to be do nothing, therefore no memset
will be called.
This is not a bug in the compiler, this is required behaviour. To remove the redundant memset
, you could just write A a;
. In this case a
is default-initialized and no automatic zeroing occurs, with or without the user-provided constructor.
† This is important since A a()
will be parsed as a function called a
with return type A
这篇关于为什么C ++使用memset(addr,0,sizeof(T))构造一个对象?标准还是编译器错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!