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

  1. char buffer[2048] ={0};
  2. string str;
  3. int n = buffer_remove(buffer,sizeof(buffer));
  4. 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. 九度oj 1034 寻找大富翁 2009年浙江大学计算机及软件工程研究生机试真题

    题目1034:寻找大富翁 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5323 解决:2123 题目描述:     浙江桐乡乌镇共有n个人,请找出该镇上的前m个大富翁. 输入:     ...

  2. Firebird Connection pool is full

    今天在做Firebird V3.0.3  x64 版本内存测试,本地PC上,准备开启800个事务(保持不关闭),每个事务做些事,尽量不释放内存. 每次测试当事务数达到时,就提示Connection p ...

  3. java 并发(五)---AbstractQueuedSynchronizer(5)

    问题 : ArrayBlockQueue 和 LinkedBlockQueue 的区别 两者的实现又是怎么样的 应用场景 BlockingQueue 概述 blockingQueue 是个接口,从名字 ...

  4. 使用axis2调用webservice需要导入的依赖

    <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> &l ...

  5. [javaEE] 三层架构案例-用户模块(二)

    使用junit测试框架,测试查找用户和添加用户功能 com.tsh.test.xmlUserDaoTest package com.tsh.test; import org.junit.Test; i ...

  6. MySql数据快速导入

    使用LOAD DATA INFILE 插入速度可以提升很多 左侧是直接导入100W花费135s ,Dos界面通过Load方式导入450W只用时23s,性能一下子显示出来了.

  7. JavaScript switch语句

    JavaScriptswitch语句 switch语句用于基于不同的条件来执行不同的动作. JavaScript switch 语句 使用switch语句可以进行多项选择. 语法: switch( 变 ...

  8. jQuery Text-to-Speech 谷歌在线语音

    <!DOCTYPE html> <html> <head> <meta content="text/html; charset=utf-8" ...

  9. Found 1 slaves: Use of uninitialized value in printf at /usr/local/percona-toolkit/bin/pt-online-schema-change line 8489

    1. problem description: as the title show, i miss the first problem using pt-online-schema-change to ...

  10. 类中调用界面ActiveX控件报错当前线程不在单线程单元中因此无法实例化 ActiveX 控件的解决办法

    解决办法是Form类中定义一个静态的ActiveX对象,在formload中将界面上的ActiveX对象赋值给新定义的对象,类中访问该静态对象即可. public static AxClientDri ...