gpt4 book ai didi

C++ 不合格名称查找 : different structure size in different cpp's leading to operator new allocating less memory than constructor processes?

转载 作者:行者123 更新时间:2023-11-27 22:35:21 25 4
gpt4 key购买 nike

例子如下:

Main.cpp :

#include "MooFoobar.h"
#include "MooTestFoobar.h"

#include "FoobarUser.h"

namespace moo::test::xxx {
struct X
{
void* operator new(const size_t size);

FoobarUser m_User;
};

void* X::operator new(const size_t size)
{
printf("Allocated size: %zd\n", size);
return malloc(size);
}
} // namespace moo::test::xxx

int main()
{
new moo::test::xxx::X;
printf("Actual size: %zd, member size: %zd\n", sizeof(moo::test::xxx::X), sizeof(moo::test::xxx::FoobarUser));
return 0;
}

MooFoobar.h :

namespace moo {
struct Foobar
{
char m_Foo[64];
};
} // namespace moo

MooTestFoobar.h :

namespace moo::test {
struct Foobar
{
char m_Foo[32];
};
} // namespace moo::test

FoobarUser.h :

#include "MooFoobar.h"

namespace moo::test::xxx {
struct FoobarUser
{
FoobarUser();
~FoobarUser();

Foobar m_Foobar;
};
} // namespace moo::test::xxx

FoobarUser.cpp :

#include "FoobarUser.h"
#include <cstdio>

moo::test::xxx::FoobarUser::FoobarUser()
: m_Foobar()
{
printf("FoobarUser constructor, size: %zd\n", sizeof(*this));
}

moo::test::xxx::FoobarUser::~FoobarUser()
{}

那么这里发生了什么:根据包含的顺序,非限定名称在不同类型和 FoobarUser.cpp 中解析。我们得到尺寸 64 , 在 Main.cpp我们得到尺寸 32 .不仅sizeof是不同的 - operator new以不正确的 ( 32 ) 大小调用,但构造函数将初始化 64 的大小, 那些导致内存损坏的。

在 clang 和 msvc 中,这个程序的结果是:

Allocated size: 32
FoobarUser constructor, size: 64
Actual size: 32, member size: 32

这听起来很可疑,基本上意味着如果有名称冲突,不合格的名称是不行的,因为根据包含顺序,它可能导致本质上是不正确的程序。

但我在 C++ std 中找不到任何一点可以说明任何无效/格式错误的代码。谁能帮帮我?

这真的是标准问题,而不是一些复杂的大规模编译器问题(尽管我真的看不出编译器如何解决这种情况)?

最佳答案

严格回答你的问题

I can't find any point in the C++ std that would say any of that invalid/ill-formed code. Can anyone help me?

这是 [basic.def.odr]/12.2

There can be more than one definition of a class type [...] in a program provided that each definition appears in a different translation unit, and provided the definitions satisfy the following requirements. Given such an entity named D defined in more than one translation unit, then

  • each definition of D shall consist of the same sequence of tokens; and
  • in each definition of D, corresponding names, looked up according to [basic.lookup], shall refer to an entity defined within the definition of D, or shall refer to the same entity, after overload resolution and after matching of partial template specialization ([temp.over]), except that a name can refer to

    • [ ... nothing of relevance]

在你的程序中,FoobarUser 在你的两个翻译单元中都定义了,但是 Foobar 中的名称指的是 --- 根据不合格查找的规则 -- - 两个不同的实体(moo::test::FooBarmoo:FooBar)。这违反了单一定义规则。无需诊断。

关于C++ 不合格名称查找 : different structure size in different cpp's leading to operator new allocating less memory than constructor processes?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55047893/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com