C++Bug记录

七月

2023/7/23

terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc

开动态数组的时候参数为一个负值或其他一些奇怪的值

2023/7/25
运行时自动跳转到new_allocator.h文件

动态数组越界

7/28

std::string line;
std::string first;              //无人机相关部分数据
std::getline(inputFile, line); 
line = line[0] + "25" + line.substr(1); first += (line + "\n\n");    //读文件名

上面的代码是有问题的,line[0]是个字符,不是字符串

八月

    const char* filename = parser.retrieve().c_str();
    std::ifstream fp;
    fp.open(filename,std::ifstream::in);

retrieve()返回string临时变量,可能会导致filename指向的内容不存在。

九月

switch(expression)里面的expression不能是string,若要判断string可以这样用

void printcolor(string color, string str)
{
  int clr;
  string head;
  string tail;
  string display;
  if(color=="red") clr=1;
  if(color=="green") clr=2;
  switch(clr){
    case 1:{
      head="\033[31m";
      tail="\033[0m";
      display=head+str+tail;
      break;
    }
    case 2:{
      head="\033[32m";
      tail="\033[0m";
      display=head+str+tail;
      break;
    }
    default:{
      display=str;
      break;
    }
  }
  cout << display << endl;
}
posted @ 2023-07-23 17:54  何太狼  阅读(8)  评论(0编辑  收藏  举报