Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C+中字符串类中的堆分配+; 在下面的C++代码中,应该有三个堆分配,如在 SWAP-()/Cuff>函数中,还创建一个临时字符串对象。为什么在这段代码中只有两个堆分配 不使用移动语义 利用移动语义_C++_String_C++11_Move_Swap - Fatal编程技术网

C+中字符串类中的堆分配+; 在下面的C++代码中,应该有三个堆分配,如在 SWAP-()/Cuff>函数中,还创建一个临时字符串对象。为什么在这段代码中只有两个堆分配 不使用移动语义 利用移动语义

C+中字符串类中的堆分配+; 在下面的C++代码中,应该有三个堆分配,如在 SWAP-()/Cuff>函数中,还创建一个临时字符串对象。为什么在这段代码中只有两个堆分配 不使用移动语义 利用移动语义,c++,string,c++11,move,swap,C++,String,C++11,Move,Swap,即使不使用移动语义,为什么只有两个堆分配?临时字符串在哪里分配内存?如果在这两种情况下都有两个堆分配,那么在这里使用std::move()的优点是什么?您很可能使用了一个根据C++98/03标准运行的实现,并为std::string实现了写时复制 现场演示: 如果是这种情况,您的问题不应被标记为c++11,因为从那时起,不允许写时复制。(通常会实现SSO,但情况不同。) 如果您切换到较新的GCC,您将看到3个分配:您能否向我们展示您在理解程序时遇到的实际困难(“不使用移动语义”)?还包括您从程序

即使不使用移动语义,为什么只有两个堆分配?临时字符串在哪里分配内存?如果在这两种情况下都有两个堆分配,那么在这里使用std::move()的优点是什么?

您很可能使用了一个根据C++98/03标准运行的实现,并为
std::string
实现了写时复制

现场演示:

如果是这种情况,您的问题不应被标记为
c++11
,因为从那时起,不允许写时复制。(通常会实现SSO,但情况不同。)


如果您切换到较新的GCC,您将看到3个分配:

您能否向我们展示您在理解程序时遇到的实际困难(“不使用移动语义”)?还包括您从程序中获得的实际输出,以及可能的预期输出。@jtbandes这不是关于SSO的,不要误导。您使用的是什么编译器?首先,我使用gcc和clang(都启用了优化)获得了不同的输出。您是否查看了生成的代码以了解它的功能?也许编译器会识别“交换”模式,并生成代码来交换指针?@ArunSuryan:GCC的哪个版本,以及编译时使用的命令行?我不能在计算机上复制;我得到了三个分配(虽然它们小得多;27字节,而不是51字节)。因此,在这两种情况下都会有三个分配,或者只有在我使用移动语义的情况下才会有三个分配?@ArunSuryan您能回答关于GCC版本的评论中的问题吗?另外,您使用哪些编译标志?无论如何,这似乎是部分C++11支持。COW在C++11中是不允许的,并且直到C++11才存在
std::move
。根据C++标准,你不应该同时使用这两种方法。它是GCC(GCC)4.8 5 20150623(Red Hat 4.85-36)@ ArunSuryan为什么应该有3个移动语义分配?移动只是“窃取”资源,即存储的字符串。为什么它应该分配任何东西?@阿伦苏扬,是的,似乎GCC 4.85混合了牛和<代码> STD::移动< /COD>特征,它不符合任何特定的C++标准。我建议你不要使用这种过时的版本。
#include <iostream>
#include <unordered_map>

using namespace std;


static uint32_t allocations = 0;

void *operator new(size_t size)
{
    allocations++;
    cout << "Allocating " << size << " bytes\n";
    return malloc(size);
}


void swapUsingMove(string& arg1, string& arg2)
{
    string temp = arg1;
    arg1 = arg2;
    arg2 = temp;
    cout << allocations << endl;
}

int main()
{
    string str1{"ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
    string str2{"ZYXWVUTSRQPONMLKJIHGFEDCBA"};

    swapUsingMove(str1, str2);
    cout << str1 << " " << str2;
    return 0;
}
Allocating 51 bytes
Allocating 51 bytes
2
ZYXWVUTSRQPONMLKJIHGFEDCBA ABCDEFGHIJKLMNOPQRSTUVWXYZ
#include <iostream>
#include <unordered_map>

using namespace std;


static uint32_t allocations = 0;

void *operator new(size_t size)
{
    allocations++;
    cout << "Allocating " << size << " bytes\n";
    return malloc(size);
}


void swapUsingMove(string& arg1, string& arg2)
{
    string temp = move(arg1);
    arg1 = move(arg2);
    arg2 = move(temp);
    cout << allocations << endl;
}

int main()
{
    string str1{"ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
    string str2{"ZYXWVUTSRQPONMLKJIHGFEDCBA"};

    swapUsingMove(str1, str2);
    cout << str1 << " " << str2;
    return 0;
}
Allocating 51 bytes
Allocating 51 bytes
2
ZYXWVUTSRQPONMLKJIHGFEDCBA ABCDEFGHIJKLMNOPQRSTUVWXYZ