簡體   English   中英

malloc錯誤“命名管道打開中的內存損壞(快速)

[英]malloc error “memory corruption (fast) in named pipe opening

我正在嘗試創建一些N號名稱管道(fifo)。 打開14 fifo后我得到​​一些stratnge錯誤。 以下是我的代碼: -

int main(int argc, char **argv)
{
   if(argc != 2)                                                                                                      
   {                                                                                                                
      fprintf(stderr, "Usage:- fifo <number of fds> \n");
      exit(1);
   }

   int i = atoi(argv[1]);
   int *socket = new int(i);
   char cc[10]; 
   std::string fifo = "event.fifo";
   for(int j=0; j<i; ++j)
   {
       fifo = "event.fifo";
       sprintf(cc, "%d", j);
       fifo += cc;

        unlink(fifo.c_str());
        if (mkfifo(fifo.c_str(), 0600) == -1) 
        {
                perror("mkfifo");
                exit(1);
        }

        socket[j] = open(fifo.c_str(), O_RDONLY | O_NONBLOCK, 0);

        if (socket[j] == -1) 
        {
                perror("open");
                exit(1);
        }
        fprintf(stderr, "Fifo created %s\n", fifo.c_str());
    }
    fprintf(stderr, "\n============================================\n");
    return 0;
}

我的程序沒有崩潰,但它給了我錯誤: -

Error in `./fifo': malloc(): memory corruption (fast): 0x09293018 

錯字:

int *socket = new int(i);

應該

int *socket = new int[i];

(您只分配了一個int而不是數組。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM