C++ 中附加的 char* 的奇怪输出
Strange outputs from appended char* in C++
我只是在写简单的代码,然后我看到了一些奇怪的东西。该代码应该将一个字符串附加到另一个字符串。新附加字符串的输出不仅输出正确的附加字符串,而且每次四次都加上数字 2,我不明白为什么。我以为是我忽略了一些内存泄漏或类似的东西,但它每次都输出相同的四个数字。
代码:
#include <iostream>
using namespace std;
unsigned int getStrSize(const char* string)
{
unsigned int size = 0;
while (string[size] != '[=10=]')
{
size++;
}
return size;
}
int main()
{
const char* bla1 = "hello";
const char* bla2 = " blaah";
int size1 = getStrSize(bla1);
int size2 = getStrSize(bla2);
int size12 = size1 + size2;
char* bla12 = new char[size12];
for (int i = 0; i < size1; i++)
{
bla12[i] = bla1[i];
}
for (int i = 0; i < size2; i++)
{
bla12[i + size1] = bla2[i];
}
char* blaNew = bla12;
cout << bla1 << "\n";
cout << bla2 << "\n";
cout << bla12 << "\n";
cout << blaNew << "\n";
}
输出:
hello
blaah
hello blaah²²²²
hello blaah²²²²
你错过了零:
#include <iostream>
using namespace std;
unsigned int getStrSize(const char* string)
{
unsigned int size = 0;
while (string[size] != '[=10=]')
{
size++;
}
return size;
}
int main()
{
const char* bla1 = "hello";
const char* bla2 = " blaah";
int size1 = getStrSize(bla1);
int size2 = getStrSize(bla2);
int size12 = size1 + size2 + 1; // notice +1
char* bla12 = new char[size12];
for (int i = 0; i < size1; i++)
{
bla12[i] = bla1[i];
}
for (int i = 0; i < size2; i++)
{
bla12[i + size1] = bla2[i];
}
bla12[size12 - 1] = '[=10=]'; // terminate with nil
char* blaNew = bla12;
cout << bla1 << "\n";
cout << bla2 << "\n";
cout << bla12 << "\n";
cout << blaNew << "\n";
delete[] bla12; // Don't leak memory
delete[] blaNew; //
}
最好考虑使用标准库函数:
#include <cstring>
#include <iostream>
int main() {
const char* bla1 = "hello";
const char* bla2 = " blaah";
auto const size1 = std::strlen(bla1);
auto const size2 = std::strlen(bla2);
auto const size12 = size1 + size2 + 1;
char* bla12 = new char[size12];
std::strcpy(bla12, bla1);
std::strcat(bla12, bla2);
char* blaNew = bla12;
std::cout << bla1 << "\n";
std::cout << bla2 << "\n";
std::cout << bla12 << "\n";
std::cout << blaNew << "\n";
delete[] bla12;
delete[] blaNew;
}
问题是您没有以 null 终止您的 char* 缓冲区。 std::cout.operator<<(char*)
将尝试找到 [=11=]
作为其终止字符。所以你只需要在缓冲区的末尾附加 [=11=]
。
注意:注意增加buffer 1的大小,否则你会access/write一个未分配的内存。
我只是在写简单的代码,然后我看到了一些奇怪的东西。该代码应该将一个字符串附加到另一个字符串。新附加字符串的输出不仅输出正确的附加字符串,而且每次四次都加上数字 2,我不明白为什么。我以为是我忽略了一些内存泄漏或类似的东西,但它每次都输出相同的四个数字。
代码:
#include <iostream>
using namespace std;
unsigned int getStrSize(const char* string)
{
unsigned int size = 0;
while (string[size] != '[=10=]')
{
size++;
}
return size;
}
int main()
{
const char* bla1 = "hello";
const char* bla2 = " blaah";
int size1 = getStrSize(bla1);
int size2 = getStrSize(bla2);
int size12 = size1 + size2;
char* bla12 = new char[size12];
for (int i = 0; i < size1; i++)
{
bla12[i] = bla1[i];
}
for (int i = 0; i < size2; i++)
{
bla12[i + size1] = bla2[i];
}
char* blaNew = bla12;
cout << bla1 << "\n";
cout << bla2 << "\n";
cout << bla12 << "\n";
cout << blaNew << "\n";
}
输出:
hello
blaah
hello blaah²²²²
hello blaah²²²²
你错过了零:
#include <iostream>
using namespace std;
unsigned int getStrSize(const char* string)
{
unsigned int size = 0;
while (string[size] != '[=10=]')
{
size++;
}
return size;
}
int main()
{
const char* bla1 = "hello";
const char* bla2 = " blaah";
int size1 = getStrSize(bla1);
int size2 = getStrSize(bla2);
int size12 = size1 + size2 + 1; // notice +1
char* bla12 = new char[size12];
for (int i = 0; i < size1; i++)
{
bla12[i] = bla1[i];
}
for (int i = 0; i < size2; i++)
{
bla12[i + size1] = bla2[i];
}
bla12[size12 - 1] = '[=10=]'; // terminate with nil
char* blaNew = bla12;
cout << bla1 << "\n";
cout << bla2 << "\n";
cout << bla12 << "\n";
cout << blaNew << "\n";
delete[] bla12; // Don't leak memory
delete[] blaNew; //
}
最好考虑使用标准库函数:
#include <cstring>
#include <iostream>
int main() {
const char* bla1 = "hello";
const char* bla2 = " blaah";
auto const size1 = std::strlen(bla1);
auto const size2 = std::strlen(bla2);
auto const size12 = size1 + size2 + 1;
char* bla12 = new char[size12];
std::strcpy(bla12, bla1);
std::strcat(bla12, bla2);
char* blaNew = bla12;
std::cout << bla1 << "\n";
std::cout << bla2 << "\n";
std::cout << bla12 << "\n";
std::cout << blaNew << "\n";
delete[] bla12;
delete[] blaNew;
}
问题是您没有以 null 终止您的 char* 缓冲区。 std::cout.operator<<(char*)
将尝试找到 [=11=]
作为其终止字符。所以你只需要在缓冲区的末尾附加 [=11=]
。
注意:注意增加buffer 1的大小,否则你会access/write一个未分配的内存。