这个问题的背景是在用libevent的buffer_remove时出现的,写一个伪代码

char buffer[2048] ={0};
string str;
int n = buffer_remove(buffer,sizeof(buffer));
str = string(buffer);

  在这里断点调试时发现buffer的数据是正确的,转到str时,总会在字符串的结尾出现几个乱七八糟的字符且每次都不一样.

下面说一下原因,首先我们都知道每个字符串是以'\0'(即0)结尾的,而buffer_remove在从libevent的内存移到我们自己的buffer里时,是不会对字符串做改变的,接收到什么就往buffer理写什么,所以在断点调试时看到buffer里有2048个字符时,实际上是没有'\0'的,之所以能看到字符串大概是因为IDE的原因?

  在string构造时,它是会找参数的的buffer *直到遇到‘\0’,并把这些深拷贝到string的成员变量char *中,所以如果我们的buffer没有'\0',在找到buffer的第2048个字符后,就会继续在不属于buffer的内存里找(buffer后的这块内存很可能是混乱地),直到找到内存为0的地方停止。比如11 1a 34 57 00,11为buffer[2047],则string构造时会把1a 34 57也拷贝到内存中,于是就出现了描述的错误

char *转string遇到诡异的问题记录的更多相关文章

  1. 对bit、byte、TByte、Char、string、进制的认识

    在学校老师就教1byte = 8bit,一个Byte在内存中占8个房间.每个房间都有门牌号.找到内存中的内容就找门牌号,寻址什么的,虽然在听,但是脑袋里一头雾水,到现在只知道会用就行,但原理也不是那么 ...

  2. char *s="string"和char s[]="string"的区别

    char *s="string"的内容是不可以改的 void main() {     char* pStr1 = "Hello!";     char pSt ...

  3. 探究 C# 中的 char 、 string(一)

    目录 探究 C# 中的 char . string(一) 1. System.Char 字符 2. 字符处理 3. 全球化 4. System.String 字符串 4.1 字符串搜索 4.2 字符串 ...

  4. string to char* and char* to string 玩转 String 和 Char*

    char 类型是c语言中常见的一个数据类型,string是c++中的一个,它的定义为 Strings are objects that represent sequences of character ...

  5. 【C++】int、const char*、char*、char、string之间的转换

    #include "stdafx.h" #include<string> #include<vector> #include<iostream> ...

  6. C++ 中int,char,string,CString类型转换

      1. c++中string到int的转换 1) 在C标准库里面,使用atoi: #include <cstdlib> #include <string> std::stri ...

  7. C++ char*,const char*,string的相互转换

    1. string转const char* string s ="abc";constchar* c_s = s.c_str(); 2. const char*转string   ...

  8. c++ stl string char* 向 string 转换的问题

    请看下面代码 string AddString(const string& a,const string & b) { return a + b; } int _tmain(int a ...

  9. C++ 中 int,char*,string,CString之间相互转换-整理

    <多字符集下> #include <string> //使用C++标准库的string类时, 定义时 std::string str; using namespace std; ...

随机推荐

  1. spring mvc中DispatcherServlet如何得到ModelAndView的

    首先看下面这种张图,这张图说明了spring mvc整体的流程. 本文讲的就是如何从DispatcherServlet中得到ModerAndView的过程. 首先看DispatherServlet这个 ...

  2. 持续集成:TeamCity 的安装和使用

    TeamCity 本文初衷 让大家了解持续集成(CI),以及入门了解 JetBrains 家的 TeamCity 的一些简单实用. TeamCity 的一些复杂使用我暂时也不会,一样也是要看文档的,所 ...

  3. Gradient descent and others

    Batch gradient descent Procedure 在循环中跌倒公式\(\theta_j:=\theta_j-\alpha{1\over{m}}\sum_{i=1}^m(h_{\thet ...

  4. 问题集录04--json和jsonp讲解

    JSON和JSONP  JSON(Javascript Object Notation)是一种轻量级的数据交换格式,用于在浏览器和服务器之间交换信息.  JSONP(JSON With Padding ...

  5. 【转】HttpURLConnection用法详解

    原文链接:http://www.blogjava.net/supercrsky/articles/247449.html 针对JDK中的URLConnection连接Servlet的问题,网上有虽然有 ...

  6. sql 表插锁 解锁

    --查锁 select request_session_id spid,OBJECT_NAME(resource_associated_entity_id) tableName from sys.dm ...

  7. MVC中FileResult 返回类型返回Excel

    公司中以前写的导出有问题.原来使用的XML格式字符串拼接然后转化成流输出 action public FileResult ExportJobFair() { try { string name = ...

  8. Go.网络篇-2

    package main import ( "io/ioutil" "os" "io" "log" "net/ ...

  9. Java中的断言 Assert

    今天正好遇到了,就记一下 一.作用: 用与编写单元测试 二.assert 关键字 assert 理论上和 if类似, 但是assert 仅仅用于测试, 不能用于业务 如果发现断言无效, 则可能时ide ...

  10. HttpResponse Entity的处理(将字符数组转为JSON)

    1.问题背景 调用高德的IP地址查询接口,获取的返回值为字符串数组(如下); 因为这里只是纯字符串,并不是真正的数组,无法直接取值,所以想到看能不能转为数组或者JSON再进行取值. 2.解决: 通过在 ...