char *转string遇到诡异的问题记录
这个问题的背景是在用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遇到诡异的问题记录的更多相关文章
- 对bit、byte、TByte、Char、string、进制的认识
在学校老师就教1byte = 8bit,一个Byte在内存中占8个房间.每个房间都有门牌号.找到内存中的内容就找门牌号,寻址什么的,虽然在听,但是脑袋里一头雾水,到现在只知道会用就行,但原理也不是那么 ...
- char *s="string"和char s[]="string"的区别
char *s="string"的内容是不可以改的 void main() { char* pStr1 = "Hello!"; char pSt ...
- 探究 C# 中的 char 、 string(一)
目录 探究 C# 中的 char . string(一) 1. System.Char 字符 2. 字符处理 3. 全球化 4. System.String 字符串 4.1 字符串搜索 4.2 字符串 ...
- string to char* and char* to string 玩转 String 和 Char*
char 类型是c语言中常见的一个数据类型,string是c++中的一个,它的定义为 Strings are objects that represent sequences of character ...
- 【C++】int、const char*、char*、char、string之间的转换
#include "stdafx.h" #include<string> #include<vector> #include<iostream> ...
- C++ 中int,char,string,CString类型转换
1. c++中string到int的转换 1) 在C标准库里面,使用atoi: #include <cstdlib> #include <string> std::stri ...
- C++ char*,const char*,string的相互转换
1. string转const char* string s ="abc";constchar* c_s = s.c_str(); 2. const char*转string ...
- c++ stl string char* 向 string 转换的问题
请看下面代码 string AddString(const string& a,const string & b) { return a + b; } int _tmain(int a ...
- C++ 中 int,char*,string,CString之间相互转换-整理
<多字符集下> #include <string> //使用C++标准库的string类时, 定义时 std::string str; using namespace std; ...
随机推荐
- Python基础(6) - 基本语句
Python print(在Python 3.0中就变成了函数了) print语句是把对象用文本化的形式输出到标准的输出流上. Operation Interpretation print spam ...
- 利用git工具命令简单的从github上拷贝和上传代码
第一:从github上拷贝项目到本地 1.在github上建立一个项目名为:MygitTest 2.在我们本地电脑上把这个项目拷贝下来:直接选择一个文件夹,右键选择git Bash here 直接 ...
- HDU 5696 ——区间的价值——————【线段树、快排思想】
区间的价值 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Subm ...
- js验证港澳居民通行证号码是否合规
需求:最近要做实名验证的功能,但是验证我们要验证严谨一点,参考了网上关于验证港澳居民通行证号码的代码,总结一下. 代码: function checkHKMacao(code){ var tip = ...
- [转]ASP.NET Web API基于OData的增删改查,以及处理实体间关系
本文转自:http://www.cnblogs.com/darrenji/p/4926334.html 本篇体验实现ASP.NET Web API基于OData的增删改查,以及处理实体间的关系. 首先 ...
- [转]ECMAScript 6 入门 -编程风格
本文转自:http://es6.ruanyifeng.com/#docs/style 编程风格 块级作用域 字符串 解构赋值 对象 数组 函数 Map结构 Class 模块 ESLint的使用 本章探 ...
- js跳转指定的网站
$(function () {window.location.replace("http:new.mingyikanya.com");});
- 创建一个自定义的Application类
由于每个应用程序必须创建一个Application对象,vs为开发人员提供了模板来减轻开发人员的重复工作.当使用vs创建一个WPF应用程序是,vs会自动创建一个app.xaml文件, <Appl ...
- C# XML相关
XmlDocument doc = new XmlDocument(); 1.string类型的xml,如何转换成xml类型 doc.LoadXml("需要传入的string类型的xml&q ...
- GeneratedKeyHolder的作用:获得新建主键值
Spring利用GeneratedKeyHolder,提供了一个可以返回新增记录对应主键值的方法: int update(PreparedStatementCreator psc, KeyHolder ...