简体   繁体   English

删除char *进行字节转换

[英]Delete char* for byte conversion

I tried to read a binary file using different length byte every time I read the file. 我每次读取文件时都尝试使用不同的长度字节读取二进制文件。 After I got the value, I try to convert the bytes to char* . 获得值后,我尝试将字节转换为char*

I created a simple code as follows: 我创建了一个简单的代码如下:

//This code is compiled in Visual Studio 2010
typedef unsigned char BYTE;

BYTE *s;
s = new BYTE[3]; // I read 2 bytes from the file, I add +1 to reserve NULL
s[0]= 'a'; s[1]='b';s[2]=NULL;  //just an example I get 2 bytes from file

char* b;
b = new char(sizeof(s));
strcpy(b,(char*)s);
s[0]='x';
cout << s <<"--"<< b<< "--"<< endl;
delete[] s;
delete[]  b;
cin.get();
return 0;`

However, the code generates error "Heap Corruption Detected". 但是,代码会生成错误“Heap Corruption Detected”。 When I removed the line, delete[] b; 当我删除该行时, delete[] b; the program runs well. 该计划运行良好。 But I am not sure the next time if the problem may arise. 但是我不确定下次是否会出现问题。 Will somebody explain about it, please? 请问有人解释一下吗? Will it cause memory leak if I remove delete[] b; 如果我删除delete[] b;会导致内存泄漏吗delete[] b; ? Any suggestions to improve my code? 有什么建议来改进我的代码吗?

This: 这个:

b = new char(sizeof(s));

Should be: 应该:

b = new char[sizeof(s)];

Otherwise you are not creating an array, you are just just creating a pointer to a char that has the character code of sizeof(a). 否则你不是在创建一个数组,只是创建一个指向字符代码为sizeof(a)的char的指针。

And therefore delete[] b is causing it to crash because you are trying to delete an array where there is no array. 因此删除[] b导致它崩溃,因为您试图删除没有数组的数组。

Also another problem, sizeof(s) is not going to give you what you want. 还有一个问题,sizeof(s)不会给你你想要的东西。 s is a dynamically allocated array so calling sizeof(s) is not going to give you the sum of the sizes of chars in s. s是一个动态分配的数组,因此调用sizeof(s) 不会给出s中chars大小的总和。 sizeof(s) will return the size of the pointer to s. sizeof(s)将指针的大小返回给s。

While David Saxon has explained the immediate reason for your error, your code could be significantly improved by the use of the C++ standard library: 虽然David Saxon已经解释了出错的直接原因,但使用C ++标准库可以显着改善您的代码:

//This code is compiled in Visual Studio 2010
typedef unsigned char BYTE;

//s will be automatically destroyed when control leaves its scope
//so there is no need for the `delete[]` later on, and there
//will be no memory leaks if the construction of `b` fails.
std::vector<BYTE> s(3);// I read 2 bytes from the file, I add +1 to reserve NULL
s[0]= 'a'; s[1]='b';s[2]=NULL;  //just an example I get 2 bytes from file
//`sizeof s` is wrong, as it gives the size of the `s` object, rather than
//the size of the allocated array.
//Here I instead just make `b` as a copy of `s`.
std::vector<BYTE> b(s);
s[0]='x';
cout << s.data() <<"--"<< b.data() << "--"<< endl;
//There is no need for `delete[] s` and `delete[] b` as `s` and `b`
//have automatic storage duration and so will automatically be destroyed.
cin.get();
return 0;`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM