C ++中static的含义 [英] The meaning of static in C++

查看:74
本文介绍了C ++中static的含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以为我对C ++相当满意,事实证明我不是.我问过一个先前的问题: C ++ const左值引用在以下答案之一中具有以下代码:

I thought I was fairly good with C++, it turns out that I'm not. A previous question I asked: C++ const lvalue references had the following code in one of the answers:


#include <iostream>
using namespace std;

int& GenX(bool reset) { static int* x = new int; *x = 100; if (reset) { delete x; x = new int; *x = 200; } return *x; }

int& GenX(bool reset) { static int* x = new int; *x = 100; if (reset) { delete x; x = new int; *x = 200; } return *x; }

class YStore { public: YStore(int& x); int& getX() { return my_x; } private: int& my_x; };

class YStore { public: YStore(int& x); int& getX() { return my_x; } private: int& my_x; };

YStore::YStore(int& x) : my_x(x) { }

YStore::YStore(int& x) : my_x(x) { }

int main() { YStore Y(GenX(false)); cout << "X: " << Y.getX() << endl; GenX(true); // side-effect in Y cout << "X: " << Y.getX() << endl; return 0; }

int main() { YStore Y(GenX(false)); cout << "X: " << Y.getX() << endl; GenX(true); // side-effect in Y cout << "X: " << Y.getX() << endl; return 0; }

上面的代码输出X:100,X:200.我不明白为什么. 我玩了一点,并增加了一些输出,即删除x之前的cout;在新的x后面有一个cout;在重置控制块中.

The above code outputs X: 100, X:200. I do not understand why. I played with it a bit, and added some more output, namely, a cout before the delete x; and a cout after the new x; within the reset control block.

我得到的是: 删除前:0x92ee018 新版本后:0x92ee018

What I got was: before delete: 0x92ee018 after new: 0x92ee018

因此,我发现static静默地失败了对x的更新,而第二个getX正在(未删除后)使用未初始化的内存.为了测试这一点,我添加了x = 0;在删除之后,在新的之前,以及另一个提示以确保x确实重置为0.是.

So, I figured that static was silently failing the update to x, and the second getX was playing with (after the delete) uninitialized memory; To test this, I added a x = 0; after the delete, before the new, and another cout to ensure that x was indeed reset to 0. It was.

那么,这是怎么回事?新的怎么返回与先前删除释放的内存完全相同的内存块?仅仅是因为这是操作系统的内存管理器决定执行的操作,还是我所缺少的关于静态的特殊东西?

So, what is going on here? How come the new returns the exact same block of memory that the previous delete supposedly free'd? Is this just because that's what the OS's memory manager decided to do, or is there something special about static that I'm missing?

谢谢!

推荐答案

这正是内存管理器决定执行的操作.如果您考虑一下,这很有意义:您刚刚释放了一个int,然后又请求了一个int ...为什么内存管理器不应该将您刚刚释放的int退还给您?

That's just what the memory manager decided to do. If you think about it, it makes a lot of sense: You just freed an int, then you ask for an int again... why shouldn't the memory manager give you back the int you just freed?

从技术上讲,当您delete时可能发生的情况是,内存管理器正在将您释放的内存块附加到空闲列表的开头.然后,当您调用new时,内存管理器将扫描其空闲列表,并在第一个条目处找到合适大小的块.

More technically, what is probably happening when you delete is that the memory manager is appending the memory block you freed to the beginning of the free list. Then when you call new, the memory manager goes scanning through its free list and finds a suitably sized block at the very first entry.

有关动态内存分配的更多信息,请参见内部存储分配" .

For more information about dynamic memory allocation, see "Inside storage allocation".

这篇关于C ++中static的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆