Linux 上的 C++ 段错误

标签 c++ linux

抱歉,我是 c++ 和 linux 的新手,我以前没有见过段错误。我可以成功编译该文件,但它只是输出段错误。

我尝试用谷歌搜索段错误,但仍然不知道如何修复我的代码。

希望有人能拯救我并告诉我出了什么问题通过具体说明我犯的错误行。 我将不胜感激!非常感谢!

#include <iostream>
#include <cstdlib>
#include <pthread.h>
#include <unistd.h>
#include <time.h>
#include <cstring>
#include <semaphore.h>

using namespace std;

#define NUM_THREADS     36
#define MAX 36

char matrix[6][6]; //result sheet

//queue elements
char queue[MAX], head=0, tail=0;


int row=0,col=0;
int runtime=1; // track the number of times we have run the dispatch 
funciton

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

void dispatch(){

    row = runtime / 6;
    sleep(1); //wait 1 second to generate column number
    col = runtime % 6 - 1;
    if (runtime % 6 == 0)
        col = 5;

    runtime++;

}


void enqueue(char c){
    queue[tail] = c;
    tail = (tail+1)%MAX ;
}

char dequeue(){
    char temp = queue[head];
    head = (head+1)%MAX ;
return temp;
}




//master thread
void *qcTask(void *args) {
//simulate product convey delay
   sleep(1);

   pthread_mutex_lock(&mutex);

   dispatch();

   sleep(rand()%6+5);

   if(rand()%100<90)
       enqueue('Q');

   else
       enqueue('U');

   pthread_mutex_unlock(&mutex);

   pthread_exit(NULL);
}   



int main (int argc, char* argv[]){


   int input = atoi(argv[1]);
    //error handling
   if(argc!=2 || input < 0 || input>36){
      cout<<"Input is illegal!";
   }

   else{
       pthread_t threads[input];


       int rc, i, count = 0;

       srand(time(NULL));

   for(i = 0;i<input;i++){

       rc = pthread_create(&threads[i], NULL, qcTask, NULL);
       if (rc){
           cout << "Error:unable to create thread," << rc << endl;
           exit(-1);

     }
   }

   for (i = 0; i < input; i++){
       rc = pthread_join(threads[i], NULL);
       if (rc){
           cout << "Error:unable to join," << rc << endl;
           exit(-1);
       }
           }

    //initize matrix with default value 'i'
   for (int i = 0; i < 6; i++){
       for (int j = 0; j < 6; j++){
           matrix[i][j] = 'I';
       }
   }

   //fill the result sheet
   for(int i=0;i<6;i++){
        for (int j = 0; j < 6; j++){
            matrix[i][j]=dequeue();
        }

   }

   for (int i = 0; i < 6; i++){
       for (int j = 0; j < 6; j++){
           cout << matrix[i][j];
           if (matrix[i][j] == 'U')
               count++; 
       }
       cout << endl;
   }

   cout << count << " products are unqualified.";
   pthread_exit(NULL);

   }
}

最佳答案

段错误(segfault/SIGSEGV)仅意味着操作系统检测到您的程序试图访问其分配的地址空间之外的内存。造成这种情况的原因可能有很多(几乎总是由代码中的错误引起)。 示例包括:

  • 取消引用未初始化的指针

  • 写入数组(或其他容器)的末尾

  • 取消引用 nullptr

  • 写入或读取已释放的内存

还有更多..

关于Linux 上的 C++ 段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43076030/

相关文章:

C++ 错误 : invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]

c++ - 通过迭代器获取集合元素的 "index"

c++ - 如何使用此特定容器在 C++ 中创建二维数组

java - 如何在 Linux 系统上使用 Java 创建 mysql 转储?

ruby - 在特定字符串之后提取搜索查询或术语的脚本

linux - 更改文件权限 Linux

c++ - 是否可以将 lambda 表达式放入 C++ 中的映射或列表中?

c++ - 范围锁定如何工作?

python - 如何获取进程的内存使用百分比?

python - 将标准错误重定向到日志,想要添加时间戳