English 中文(简体)
st st : : str str ? value value value value value value ? ? ?
原标题:std::string.c_str() has different value than std::string?

我一直在与C++号星座合作,并试图装上<条码>,*,载于<条码>以下: 采用C类功能,例如strcpy()。 自strcpy()起 <代码>*>*,作为参数,我必须投下如下内容:

std::string destination;
unsigned char *source;
strcpy((char*)destination.c_str(), (char*)source);

The code works fine and when I run the program in a debugger, the value of *source is stored in destination, but for some odd reason it won t print out with the statement

<代码>std:cout << Point;

我注意到,如果我使用的话

std:cout <<apac_str(>);

价值印本正确无误,所有价值都很好。 为什么发生这种情况? 是否有更好的方法将<条码>未签署文件*或<条码>印成<条码>; 似乎只是在我指定一个复印操作中的“条码”(<>foo.c_str()”时才会发生。

Edit: To answer the question "why would you do this?", I am using strcpy() as a plain example. There are other times that it s more complex than assignment. For example, having to copy only X amount of string A into string B using strncpy() or passing a std::string to a function from a C library that takes a char * as a parameter for a buffer.

最佳回答

这不是一个好的想法,原因有两个:

  1. destination.c_str() is a const pointer and casting away it s const and writing to it is undefined behavior.
  2. You haven t set the size of the string, meaning that it won t even necessealy have a large enough buffer to hold the string which is likely to cause an access violation.

std::string has a constructor which allows it to be constructed from a char* so simply write:

<代码>std: 显示目的地 = 来源

问题回答

Here s what you want

 std::string destination = source;

您在这么多层次上做了哪些工作是错误的......您对“<条码>的内在表述进行了重新撰写:扼制。 我指的是......不要冷却......它比它更加复杂,阵列正在改写,只读......工程。

你们正在做的是没有界定的行为。 您的C_str(s)交还了一张 con子,并非要分配给他。 为什么不使用定义的建筑或转让经营人。

你们不做什么!

我重复!

http://www.

当你做一些令人厌恶的事情时,似乎会把工作分类,这是执行扼杀性阶级的结果。 你几乎肯定是写了字,以缅怀你们的 be子和其他 b子。

当你需要与C级职能互动时,需要两种基本方法:

std::string read_from_sock(int sock) {
    char buffer[1024] = "";

    int recv = read(sock, buffer, 1024);
    if (recv > 0) {
      return std::string(buffer, buffer + recv);
    }
    return std::string();
}

或者你可以尝试石灰ek法:

std::string read_from_sock(int sock) {

    int recv = read(sock, 0, 0, MSG_PEEK);
    if (recv > 0) {
      std::vector<char> buf(recv);
      recv = read(sock, &buf[0], recv, 0);
      return std::string(buf.begin(), buf.end());
    }
    return std::string();
}

当然,这并不是很强的版本,而是说明了这一点。

首先,您应指出,通过<代码>c_str退还的价值为const char*,不得修改。 实际上,它甚至不必指出<条码>载的内部缓冲。

回答您的话题:

只需要复制X号,用地块将体积A缩入方。

如果扼杀A是一种果园,并且扼杀B:扼杀和str(A)”和“X”,那么你可以这样做:

B.assign(A, A + X);

passing a std::string to a function from a C library that takes a char * as a parameter for a buffer

如果参数实际上为<代码>const char*,则您可为此使用c_str(<>>/code>。 但是,如果这只是一个简单的<代码>char *,而且你正在使用一个C++11合规的汇编器,那么你可以做以下工作:

c_function(&B[0]);

然而,你需要确保数据在校方留有余地(与你使用直线显示器一样),你可以要求<条码>实现。 如果该职能写给被扼杀的不明特性,那么你很可能想把扼杀的后推倒下来,例如:

B.resize(B.find(  ));

在C++11汇编者而不是C++03汇编者中,你可以安全地这样做的原因是,在C++03中,地壳没有得到连续标准的保障,而在C++11,则得到保证。 如果你希望C++03号文件的保证,那么你可以使用<代码>std:vector<char>。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签