在全局范围内调用 new 运算符

发布于 2024-12-14 10:00:00 字数 451 浏览 2 评论 0原文

我和一位同事正在争论在全局范围内编写此代码的编译性:

int* g_pMyInt = new int;

我的争论围绕着调用函数(new 是)这一事实 在全球范围内是不可能的。令我惊讶的是,上面的行编译得很好 (MS-VC8 和 Apple 的 LLVM 3)。

所以我继续尝试:

int* foo()
{
    return new int;
}
int* g_pMyInt = foo(); // Still global scope.

而且,它也编译好了并且工作得像一个魅力(稍后用一个类进行了测试) whos 构造函数/析构函数打印出一条消息。演员的留言 通过了,医生没有。那次不太惊讶。)

虽然这对我来说似乎非常错误(没有有序/正确的方式/时间来调用删除), 编译器不禁止它。为什么?

A colleague and I were arguing the compilability of writing this at global scope:

int* g_pMyInt = new int;

My arguments revolved around the fact that calling a function (which new is)
at global scope was impossible. To my surprise, the above line compiled just fine
(MS-VC8 & Apple's LLVM 3).

So I went on and tried:

int* foo()
{
    return new int;
}
int* g_pMyInt = foo(); // Still global scope.

And, that compiled as well and worked like a charm (tested later with a class
whos constructor/destructor printed out a message. The ctor's message
went through, the dtor's didn't. Less surprised that time.)

While this appears very wrong to me (no orderly/right way/time to call delete),
it's not prohibited by the compiler. Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

纸短情长 2024-12-21 10:00:00

为什么应该被允许?您所做的只是初始化一个全局变量,我们完全欢迎您这样做,即使初始化涉及函数调用:

int i = 5 + 6;

double j(std::sin(1.25));

const Foo k = get_my_foo_on(i, 11, true);

std::ostream & os(std::cout << "hello world\n");

int * p(new int);                // fine but very last-century
std::unique_ptr<int> q(new int); // ah, welcome to the real world

int main() { /* ... */ }

当然,您需要担心删除动态分配的对象,无论是它们是否在全局范围内分配...拥有资源的包装类(例如unique_ptr)将是理想的解决方案。

Why shouldn't it be allowed? All you're doing is initializing a global variable, which you are perfectly welcome to do, even if the initialization involves a function call:

int i = 5 + 6;

double j(std::sin(1.25));

const Foo k = get_my_foo_on(i, 11, true);

std::ostream & os(std::cout << "hello world\n");

int * p(new int);                // fine but very last-century
std::unique_ptr<int> q(new int); // ah, welcome to the real world

int main() { /* ... */ }

Of course you'll need to worry about deleting dynamically allocated objects, whether they were allocated at global scope or not... a resource-owning wrapper class such as unique_ptr would be the ideal solution.

素罗衫 2024-12-21 10:00:00

C++ 允许在 main 函数之前和之后进行处理,特别是对于具有构造函数和构造函数的静态对象。析构函数(它们的构造函数必须在 main 之前运行,它们的析构函数必须在其之后运行)。事实上,执行顺序并没有明确定义。

如果您使用的是 GCC,另请参阅其 构造函数 函数属性(可能帮忙下单)。

C++ allow processing to happen before and after the main function, in particular for static objects with constructors & destructors (their constructor have to run before main, their destructor after it). And indeed, the execution order is not well defined.

If you are using GCC, see also its constructor function attribute (which may help to give an order).

﹉夏雨初晴づ 2024-12-21 10:00:00

当然,您可以从全局范围调用函数,作为全局对象初始化的一部分。如果不能,则无法使用构造函数定义类型的全局变量,因为构造函数也是函数。但是请注意,不同翻译单元之间的初始化顺序没有明确定义,因此如果您的函数依赖于另一个翻译单元的全局变量,除非您采取特殊的预防措施,否则您将遇到麻烦。

Of course you can call functions from global scope, as part of the initialization of global objects. If you couldn't, you couldn't define global variables of types with constructors, because constructors also are functions. However be aware that the initialization order between different translation units is not well defined, so if your function relies on a global variable from another translation unit, you will be in trouble unless you took special precaution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文