【问题标题】:SegFault in Producer/Consumer model生产者/消费者模型中的 SegFault
【发布时间】:2012-11-28 01:00:44
【问题描述】:

我又回来了一个我似乎无法克服的段错误。

我已经弄清楚它到底是什么,它与 char* 字符串行有关。我用它来分解字节以获取此 pdf 文件以用于学校作业。

感谢您的任何帮助!

void* consumer(void *temp)
{
int* stuff = reinterpret_cast<int*>(temp);
int x = *stuff;
char* string[];
stringstream stream1;
stringstream stream2;
int temp1=0;
int temp2=0;
int sent1=0;
int sent2=0;
ofstream fout;

strcpy(string,request); //SEGFAULT(11) ON THIS LINE, WHEN CALLING "string"
strcat(string,"Byte Range: ");
...

完整的代码可以在这里找到; https://www.dropbox.com/sh/dt90ot3z4v5nruy/1H9a5Cyb5A mgetweb.h 和 mgetweb.cpp

【问题讨论】:

    标签: c++ pthreads segmentation-fault producer-consumer


    【解决方案1】:

    你还没有new 用于字符串指针的内存,访问它是未定义的行为。

    //  char* string[]; I guess that's not what you intent to do, declaring an array of pointers?
    
    char* string = new char[BIG_ENOUGH_SIZE];
    strcpy(string, request);
    

    【讨论】:

      猜你喜欢
      • 2012-10-20
      • 2012-01-08
      • 2015-10-12
      • 1970-01-01
      • 1970-01-01
      • 2015-09-22
      • 1970-01-01
      • 2011-10-27
      • 1970-01-01
      相关资源
      最近更新 更多