std :: string :: c_str()覆盖了函数返回的前一个 [英] std::string::c_str() overwrittes the previous one returned by a function

查看:127
本文介绍了std :: string :: c_str()覆盖了函数返回的前一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白当文本大小相等时指针如何相同.似乎firstStringObj :: c_str()覆盖了前一个指针.

I could not understand that how can be the pointers same when the text size is equal. It seems like firstStringObj::c_str() overwrittes the previous one's pointer.

#include <iostream>
#include <string>
#include <string>
#include <stdio.h>

std::string getConstCharAndModifyItWithANewString( const char* constchar )
{
    std::string stringAtStack( constchar );
    stringAtStack += "::isModified";
    return stringAtStack;
}

int main()
{
    const char* firstConstCharPointer = getConstCharAndModifyItWithANewString("Hi!").c_str();
    std::string firstStringObj = "Hi+";

    printf(" firstConstCharPointer(%s)(%p)\nfirstStringObj(%s)(%p)\n\n", firstConstCharPointer,firstConstCharPointer, firstStringObj.c_str(),     firstStringObj.c_str()  );
}

输出: firstConstCharPointer(Hi +)(0x4593eb8) firstStringObj(Hi +)(0x4593eb8)

OUTPUT: firstConstCharPointer(Hi+)(0x4593eb8) firstStringObj(Hi+)(0x4593eb8)

推荐答案

您的函数返回一个临时 std::string对象.分配firstConstCharPointer变量并完成表达式后,该临时对象将被销毁,释放其分配的内存块,并使该变量指向已释放的内存.这称为悬挂指针.

Your function returns a temporary std::string object. After the firstConstCharPointer variable is assigned and the expression is complete, that temporary object gets destroyed, freeing its allocated memory block and leaving the variable pointing at freed memory. This is known as a dangling pointer.

firstStringObj分配一个新的内存块,发生将重新使用临时std::string先前已分配并释放的同一内存块.因此,悬空的指针现在再次指向有效内存.这就是为什么您的printf()语句能够为两个字符串显示相同的内存地址和内容.

firstStringObj then allocates a new memory block, which happens to be reusing the same memory block that the temp std::string had previously allocated and freed. So the dangling pointer happens to now be pointing at valid memory again. That is why your printf() statement is able to display the same memory address and content for both strings.

但这是未定义的行为.每次分配的内存块完全取决于字符串的Allocator来决定.第二个std::string可以很容易地分配了一个完全不同的内存块,然后当它试图取消引用仍然指向无效内存的悬空指针时,代码更有可能崩溃,或者至少出现打印垃圾.

But this is undefined behavior. The memory block that gets allocated each time is completely up to the string's Allocator to decide. The second std::string could just as easily have allocated a completely different memory block, and then the code would be more likely to crash, or at least print garbage, when it tries to dereference the dangling pointer that is still pointing at invalid memory.

为了使代码正常工作,您需要将firstConstCharPointer更改为std::string对象,以便正确复制临时std::string,例如:

In order for your code to work, you need to change firstConstCharPointer to a std::string object so the temp std::string gets copied properly, eg:

#include <iostream>
#include <string>
#include <cstdio>

std::string getConstCharAndModifyItWithANewString( const char* constchar )
{
    std::string stringAtStack( constchar );
    stringAtStack += "::isModified";
    return stringAtStack;
}

int main()
{
    const std::string firstConstStringObj = getConstCharAndModifyItWithANewString("Hi!");
    std::string secondStringObj = "Hi!";

    std::printf(" firstConstStringObj(%s)(%p)\nsecondStringObj(%s)(%p)\n\n", firstConstStringObj.c_str(), firstConstStringObj.c_str(), secondStringObj.c_str(), secondStringObj.c_str());
}

这篇关于std :: string :: c_str()覆盖了函数返回的前一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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