Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.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++_String - Fatal编程技术网

C++ 为什么我的结构包含字符串时会崩溃?

C++ 为什么我的结构包含字符串时会崩溃?,c++,string,C++,String,我遇到了一个问题,我认为了解std::string非常有意义 我用c设计了一个队列(无锁队列),代码是: kfifo.c #包括 #包括 #包括 #包括 #定义最小值(X,Y)((X)索引[51]=电气化; 先进先出->索引[34]=大小缓冲); fifo->index[17]=fifo->index[0]=fifo->index[34]=fifo->index[51]=0; //fifo->buffer=NULL; } 静态内联无符号整数kfifo\u avail\u int(结构kfifo*

我遇到了一个问题,我认为了解std::string非常有意义

我用c设计了一个队列(无锁队列),代码是: kfifo.c

#包括
#包括
#包括
#包括
#定义最小值(X,Y)((X)<(Y)?(X):(Y))
结构kfifo{
char*buffer;//数据地址指针
无符号整数索引[96];
};
静态无符号长取整\u pow\u of_2(无符号长n){//计算n的最近的pow(2)值
//因此,%运算可以被更快的位运算取代
无符号长rval=1;
而(rval缓冲区=缓冲区;
先进先出->索引[51]=电气化;
先进先出->索引[34]=大小<2?0:大小;
}
int kfifo_alloc(结构kfifo*fifo,无符号int size,size_t elemSize){
/* 
*四舍五入到2的下一次幂,因为我们的“让指数
*“包裹”tachnique仅在这种情况下有效。
*/
如果(!是2(尺寸)的功率){
尺寸=2(尺寸)的综合功率;
}
//分配内存
char*buffer=(char*)malloc(size*elemSize);
如果(!缓冲区)
返回0;
//初始化结构
kfifo_init(先进先出、缓冲区、大小、电气化);
返回1;
}
void kfifo_free(结构kfifo*fifo){//free资源
自由(fifo->缓冲);
fifo->index[17]=fifo->index[0]=fifo->index[34]=fifo->index[51]=0;
//fifo->buffer=NULL;
}
静态内联无符号整数kfifo\u avail\u int(结构kfifo*fifo){//fifo的左空格
返回fifo->索引[34]-(fifo->索引[17]-fifo->索引[0]);
}
静态内联无符号整数kfifo_in_数据(结构kfifo*fifo,字符*from){
无符号整数off_int=(fifo->index[17]&(fifo->index[34]-1));//下一个到位的整数
无符号整数l=min(1,(fifo->索引[34]-off_int));
memcpy(fifo->buffer+off_int*fifo->index[51],from,l*fifo->index[51]);
memcpy(fifo->buffer,from+l*fifo->index[51],(1-l)*fifo->index[51]);
if(fifo->buffer+off_int*fifo->index[51]==NULL){//if以意外的顺序执行此命令
返回0;
}
返回1;
}
无符号整数kfifo_push(结构kfifo*fifo,字符*buffer){
int len=最小值(kfifo\u avail\u int(fifo),1);
如果(!kfifo_in_数据(fifo,缓冲区)){
返回0;
}
先进先出->索引[17]+=len;
回程透镜;
}
静态内联无符号整数kfifo_out_数据(结构kfifo*fifo,字符*to){
无符号整数off=(fifo->索引[0]&(fifo->索引[34]-1));
无符号整数l=min(1,(fifo->索引[34]-off));
memcpy(to,fifo->buffer+off*fifo->index[51],l*fifo->index[51]);
memcpy(to+l*fifo->index[51],fifo->buffer,(1-l)*fifo->index[51]);
//char*p=fifo->buffer+off*fifo->index[51];
//p=NULL;//将输出位置重置为NULL
返回1;
}
无符号整数kfifo_get(结构kfifo*fifo,字符*buffer){
int len=min(先进先出->索引[17]-先进先出->索引[0],1);
如果(!kfifo输出数据(fifo,缓冲区)){
返回0;
}
先进先出->索引[0]+=len;
回程透镜;
}
<>和为了在C++中使用它,我有一个包装器: 先进先出队列

#include "kfifo_ing.c"

template <typename T>
class FIFO_Queue {
 public:
  FIFO_Queue(int size) {
    kfifo_alloc(&k, size, sizeof(T));
  }
  ~FIFO_Queue() {
    // kfifo_free(&k);
  }
  bool push(const T& t) {
    return kfifo_push(&k, (char*)(&t));
  }

  bool pop(T& t) {
    return kfifo_get(&k, (char*)(&t));
  }
 private:
  kfifo k;
};
#包括“kfifo_ing.c”
模板
类先进先出队列{
公众:
先进先出队列(整数大小){
kfifo_alloc(&k,尺寸,尺寸(T));
}
~FIFO_队列(){
//kfifo_免费(&k);
}
布尔推送(常数T&T){
返回kfifo_push(&k,(char*)(&t));
}
布尔波普(T&T){
返回kfifo_get(&k,(char*)(&t));
}
私人:
kfifo k;
};
我已经测试了这个队列,我认为当元素是simple struct的int时它是正确的

但是当struct有std::string对象时会发生奇怪的事情,下面是我的测试代码:

#include "fifo_queue.h"
#include <string>
using namespace std;

struct huang {
  int a;
  string b;  // if dont have b, everything is good, but once add it, crashed!!!!!!!!
  double c;
  void Show(FILE* stream) const {
    fprintf(stream, "%d %lf\n", a, c); 
  }
};

FIFO_Queue<huang>q(80);

int main() {
  huang h;
  h.a = 1;
  q.push(h);
  q.pop(h);
  q.pop(h); // if no this, wont crash!!!!
  return 0;
}
#包括“fifo_queue.h”
#包括
使用名称空间std;
结构黄{
INTA;
字符串b;//如果没有b,一切都好,但一旦添加它,崩溃了!!!!!!!!
双c;
无效显示(文件*流)常量{
fprintf(流,“%d%lf\n”,a,c);
}
};
先进先出(80);
int main(){
黄h;
h、 a=1;
q、 推(h);
q、 pop(h);
q、 pop(h);//如果没有这个,就不会崩溃!!!!
返回0;
}
代码崩溃了,当我使用gdb检查堆栈时,它警告我~basic_string()中发生了错误, 我很困惑,我知道字符串是一个复杂的设计数据结构,它有两个指针。 但有人能解释这一点吗?它是由双重自由造成的吗?有什么方法可以让它工作吗

int main()
{
    huang h;
    h.a = 1;
    q.push(h);
    q.pop(h);
    q.pop(h); // if no this, wont crash!!!!
    return 0;
}

在您的代码中,您只在
q
上按了1个
huang
。但是你跳了两次。我怀疑您在第二次弹出中放入的
h
中没有在
huang
中正确构造的
字符串b
。当您离开
h
的作用域时,
字符串b
会被破坏,可能会访问不属于您的进程的内存,并因此崩溃。

Huang,您的代码很难理解。你能试着简化一些吗?你正在混合C和C++。可能问题在于内存分配,您可能在某个地方释放了两倍的空间。尝试在fifo结构中使用模板,而不是当前正在执行的字符指针转换。尝试使用std::函数和数据类型来简化代码。我知道您希望学习如何实现队列,而不是使用std::queue,但我们需要简化事情。:)@TomazCanabrava谢谢Tomaz,很抱歉代码混乱,我会说清楚的。读不好,我道歉C++ >代码>黄页>代码>结构不允许代码> MeMCPY < /C>。因为它有一个不平凡的C++成员变量。
int main()
{
    huang h;
    h.a = 1;
    q.push(h);
    q.pop(h);
    q.pop(h); // if no this, wont crash!!!!
    return 0;
}