Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;_C++_Dynamic Arrays_Char Pointer - Fatal编程技术网

C++ C++;

C++ C++;,c++,dynamic-arrays,char-pointer,C++,Dynamic Arrays,Char Pointer,我只是在写简单的代码,然后我看到了一些奇怪的东西。代码应该将一个字符串附加到另一个字符串。新附加字符串的输出不仅输出正确的附加字符串,而且每增加四次数字2,我不明白为什么。我以为是我忽略了什么内存泄漏之类的,但它每次都输出相同的四个数字 代码: 你错过了零: #include <iostream> using namespace std; unsigned int getStrSize(const char* string) { unsigned int size = 0;

我只是在写简单的代码,然后我看到了一些奇怪的东西。代码应该将一个字符串附加到另一个字符串。新附加字符串的输出不仅输出正确的附加字符串,而且每增加四次数字2,我不明白为什么。我以为是我忽略了什么内存泄漏之类的,但它每次都输出相同的四个数字

代码:


你错过了零:

#include <iostream>

using namespace std;
unsigned int getStrSize(const char* string)
{
    unsigned int size = 0;
    while (string[size] != '\0')
    {
        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] = '\0'; // 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; // 
}
#包括
使用名称空间std;
无符号整型getStrSize(常量字符*字符串)
{
无符号整数大小=0;
while(字符串[大小]!='\0')
{
大小++;
}
返回大小;
}
int main()
{
const char*bla1=“你好”;
const char*bla2=“blaah”;
int size1=getStrSize(bla1);
int size2=getStrSize(bla2);
int size12=size1+size2+1;//注意+1
char*bla12=新字符[size12];
对于(int i=0;icout问题是您没有空终止您的char*缓冲区。
std::cout.operatorNah我想构建自己的字符串,不依赖任何标准库函数。
hello
 blaah
hello blaah²²²²
hello blaah²²²²
#include <iostream>

using namespace std;
unsigned int getStrSize(const char* string)
{
    unsigned int size = 0;
    while (string[size] != '\0')
    {
        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] = '\0'; // 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;
}