例如,如果动态存储器的解除分配总是在与分配相反的方向上完成.在这种情况下,是否可以保证堆不会碎片化?
从理论的角度来看:对于一个非常重要的应用程序来说,是否存在一些管理内存以完全避免堆碎片的现实方法?(在堆中的每次原子更改之后,堆仍然是未分段的吗?)
当shared_ptr重置为包含的同一地址时,是否可以保证weak_ptr将过期?
#include <cassert>
#include <memory>
int main()
{
int* i = new int(0);
std::shared_ptr<int> si( i );
std::weak_ptr<int> wi = si;
si.reset( i );
assert( wi.expired() ); // wi.expired() == true (GCC 4.7)
}
Run Code Online (Sandbox Code Playgroud)
或者,当wi.expired()
未定义值时是这种情况?
编辑:
我现在稍微修改一下这个问题:
是否保证weak_ptr
在shared_ptr
重置到同一地址时会过期,该地址包含shared_ptr
初始化时的地址weak_ptr
?
#include <cassert>
#include <memory>
int main()
{
int* i = new int(0);
std::shared_ptr<int> si( i );
std::weak_ptr<int> wi = si;
si.reset();
int* j = new int(0);
// Suppose that new returns the …
Run Code Online (Sandbox Code Playgroud) C++ 有很好的特性,允许在块的末尾调用一些代码 - 析构函数。昨天我随机发现了一个技巧,它在 C 中的工作原理类似于 C++ 中的析构函数。为了说明,这里是使用这个技巧来自动关闭文件:
#include <stdio.h>
#define AUTO_FILE( id, name, access ) \
for ( FILE* id = fopen( name, access ); id != NULL; fclose( id ), id = NULL )
int main()
{
AUTO_FILE( f, "hello.txt", "w" ) {
fprintf( f, "Hello World!" );
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我认为这很好,可以有有趣的用法。例如,如果你将它与堆栈数据结构结合起来,你可以直接在源代码中声明非常直观和可读的树结构:
frame("frm") {
menu("mnu") {
popupmenu("file", "File") {
menuitem("exit", "Exit");
}
popupmenu("help", "Help") {
menuitem("about", "About...");
}
}
label("lbl", "Hello World!");
button("btn", "Push me!", btn_onclick);
} …
Run Code Online (Sandbox Code Playgroud) 如果我有这个代码:
#include <assert.h>
class Foo {
public:
bool is_static();
bool is_stack();
bool is_dynamic();
};
Foo a;
int main()
{
Foo b;
Foo* c = new Foo;
assert( a.is_static() && !a.is_stack() && !a.is_dynamic());
assert(!b.is_static() && b.is_stack() && !b.is_dynamic());
assert(!c->is_static() && !c->is_stack() && c->is_dynamic());
delete c;
}
Run Code Online (Sandbox Code Playgroud)
是否有可能实现is_stack
,is_static
,is_dynamic
方法,以便做到这一点被断言实现?
使用示例:计算Foo类型的特定对象在堆栈上使用但不计算静态或动态内存的内存大小
能够在我的代码库中轻松地对搜索某些对象的构造函数的地方进行文本搜索会很高兴.这是模式波纹管.而不是经典:
Object val( a, b );
到处使用会很好:
auto val = Object( a, b );
这样我就可以使用简单的文本搜索"Object("
并获取我调用构造函数的地方列表Object
.它的语法也不那么模糊,因此更容易制作简单的工具来自动化一些代码转换.它优雅地避免了"最令人烦恼的解析"问题.
我唯一担心的是对性能可能产生的影响.案例2)和案例1)一样快吗?(如果我们可以假设Object
已经正确定义了移动构造函数和移动赋值运算符并且启用了基本的编译器优化.)
在C++中,您可以这样做:
for (int i = 0 ; i < 10 ; i++) {
/* ... */
}
Run Code Online (Sandbox Code Playgroud)
然后变量i
只存在于for语句的body体内.用while语句存在某种方式吗?例如,做这样的事情会很好:
while ( (int c = fgetc(file)) != EOF ) {
/* ... */
}
Run Code Online (Sandbox Code Playgroud)
显然这不起作用.但是存在一些语法技巧来有效地做到这一点(变量用于while条件并且只在while语句体内可见)?
我有这个简单的代码:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string s = "1,0";
string result;
//result.resize(s.length());
replace_copy(s.begin(), s.end(), result.begin(), ',', '.');
cout << '"' << result << '"' << endl;
cout << '"' << result.c_str() << '"' << endl;
cout << result.length() << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
result.resize
取消注释行的此程序的控制台输出是:
"1.0" "1.0" 3
-thats好的,但是当line with result.resize
被注释掉时,输出是:
"" "1.0" 0
- 这会导致奇怪的错误,因为result != result.c_str()
!!!
这种行为replace_copy
(以及可能也是类似的模板)可能会被视为标准库中的错误吗?我找不到任何与此主题相关的内容.谢谢.
编译器:mingw32-g ++ 4.7.1
这段代码:
#include <stdio.h>
static void swap(unsigned char& a, unsigned char& b)
{
unsigned char t = a;
a = b;
b = t;
}
int main()
{
const unsigned bgr = 0x0000FF;
swap(((unsigned char*) &bgr)[0], ((unsigned char*) &bgr)[2]);
printf("0x%06X\n", bgr);
}
Run Code Online (Sandbox Code Playgroud)
输出:
0x0000FF
交换的变化在哪里?
这个C++代码适用于我的平台和编译器(Windows,GCC 4.7):
#include <stdio.h>
class A {
public:
/* ... */
int size() const
{
if ( this == NULL ) {
return 0;
}
return m_size;
}
private:
int m_size;
};
int main()
{
A* a = NULL;
printf( "%d\n", a->size() );
}
Run Code Online (Sandbox Code Playgroud)
但这段代码是有效的标准C++还是可移植的?方法接受这个== NULL是否合适?
c++ ×9
c ×1
c++11 ×1
constructor ×1
destructor ×1
shared-ptr ×1
stl ×1
string ×1
swap ×1
weak-ptr ×1