VC ++函数字符串:: c_str():第一个字节的地址设置为0(与g ++比较) [英] VC++ function string::c_str(): the address of the first byte was set to 0 (compare to g++)

查看:203
本文介绍了VC ++函数字符串:: c_str():第一个字节的地址设置为0(与g ++比较)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试获取字符串结果c_str()的结果与g ++不一致时,我遇到了一个奇怪的问题.

I met a strange problem when trying to get the result of a string’s function c_str() whose result is inconsistent with g++.

有一个称为Test的函数可返回字符串实例.我想使用char*类型存储结果(需要).如您所见,该函数很简单,返回一个字符串"resultstring".但是当我尝试获得结果时,发生了一些奇怪的事情.

There is a function called Test to return a string instance. And I want to use a char* type to store the result (it’s needed). As you can see the function is simple return a string "resultstring". But when I try to get the result something strange happened.

在第二部分中,我得到的结果是".第一部分和第三部分都返回结果字符串".在Visual Studio中.用g ++编译的同一代码的三部分都返回结果字符串".让我们先来看一下结果:

The result I got is "" in part two. The part one and part three both return the "resultstring". While that’s in Visual Studio. The three part of the same code compiled with g++ both return the "result string. Let’s just as well see the result first:

vs的结果:

地址:16841988

address:16841988

结果字符串

地址:16842096

address:16842096

这是空行"

地址:16842060

address:16842060

地址:16842144

address:16842144

地址:16842396

address:16842396

地址:16842396

address:16842396

结果字符串

g ++的结果

地址:5705156

address:5705156

结果字符串

地址:5705156

address:5705156

结果字符串

地址:5705156

address:5705156

地址:5705196

address:5705196

地址:5705156

address:5705156

地址:5705156

address:5705156

结果字符串

#include <iostream>
#include <string>
using namespace std;

string Test()
{
     char a[64] = "resultstring";
     return  string(a);
}
int main(void)
{
    //part one
    cout << "address:"<< (unsigned)Test().c_str() << endl;
    cout << Test().c_str() << endl;

    //part two
    char *j  = const_cast<char*>(Test().c_str());
    cout << "address:"<< (unsigned)Test().c_str() << endl;

    cout << j << endl;
    cout << "address:" << (unsigned)j <<endl;

    //part three
    string h3 = Test();
    char* j2 = const_cast<char*>(h3.c_str());
    cout << "address:"<< (unsigned)Test().c_str() << endl;
    cout << "address:"<< (unsigned)h3.c_str() << endl;

    cout << "address:" << (unsigned)j2 <<endl;
    cout << j2 <<endl;
    getchar();
    return 0;

}

现在我有三个问题.

1,为什么由g ++编译的结果返回所有resultstring,而Visual Studio的结果返回除变量j之外的所有resultstring?如果您对此进行调试,您会发现VC ++仅将j2的地址设置为00 65 73 75 …,而00 65 73 75 …esultstring并带有00起始地址.而且我们会得到"也就不足为奇了.就像char* str = "\0something else"一样,您总会得到".但是问题是,为什么只有j会发生这种情况?

1st, why the result complied by g++ returns all resultstring while the result of Visual Studio returns all resultstring except for variable j? If you debug into this you’ll find that VC++ only set the address of j2 like 00 65 73 75 … which is esultstring with a 00 begin address. And it is not strange that we’ll get "". It’s just like char* str = "\0something else" you’ll always get "". But the question is why does this happen only with j?

2,为什么(unsigned) Test ().c_str()的地址之一与其他地址不同?如果我们删除string h3 = Test ()行,则地址将全部相同.

2nd, why does one of the addresses of the (unsigned) Test ().c_str() is different with others? If we remove the line string h3 = Test () the address will be all the same.

3,返回变量j的"值是否是Visual Studio的正确"行为?为什么与g ++不同?

3rd, Is it the "correct" behavior of Visual Studio returning "" value of variable j? why it is different with g++?

期待您的答复.

关于, 凯文

推荐答案

您有未定义的行为. Test()返回的std::string是临时的,并且c_str()返回的指针(存储在j中)在临时生存期结束后不再有效.这意味着任何事情都可能发生.指针指向的数组可能包含垃圾,它可能是原始字符串,或者实现可能以null结尾.访问它可能会导致分段错误,或者它可能使您可以访问原始字符串数据.在标准库的不同编译器和实现之间,这通常可能会有所不同.

You have undefined behavior. The std::string returned by Test() is a temporary and the pointer returned by c_str() (stored in j) is no longer valid after the lifetime of the temporary ends. This means that anything can happen. The array the pointer points to may contain garbage, it may be the original string or the implementation may have null terminated the beginning of it. Accessing it may cause a segmentation fault or it may allow you to access the original string data. This can and usually does vary between different compilers and implementations of the standard library.

char *j  = const_cast<char*>(Test().c_str());
// The contents pointed to by j are no longer valid and access that content 
// is undefined behavior
cout << "address:"<< (unsigned)Test().c_str() << endl;

调用Test()的地址不同,因为每次调用它都会返回一个临时地址.一些编译器可能会对此进行优化,并且/或者数据分配可能会获得相同的内存块,但是不能保证相同.

The address is different between calls to Test() because it returns a temporary each time you call it. Some compilers may optimize this and/or the allocation of data may get the same block of memory but it is not guaranteed to be the same.

这篇关于VC ++函数字符串:: c_str():第一个字节的地址设置为0(与g ++比较)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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