簡體   English   中英

gdb輸出和終端輸出之間的差異

[英]discrepencies between gdb output and terminal output

我遇到了一個奇怪的問題。 我正在為基本上模仿生產者使用者問題的OS類制作一個共享內存fifo隊列。 在我的一個函數putBuffer() ,它向共享緩沖區插入了一個項目,在某個點之后,我沒有得到任何輸出,因此我通過gdb運行了它,它打印出我認為在初始終端運行以及退出gdb時會打印的內容它說程序正常退出,所以我不確定我的錯誤在哪里。 有沒有其他人經歷過這樣的事情?

因此,當我通過gdb運行它時,它會打印“經過初始檢查,而fifo->[12]值只是在此處設置了硬編碼值以進行測試。但是在終端中,它僅打印“使其經過初始檢查”校驗。 我什至確定該錯誤不在printf()什么想法嗎? 繼承人代碼

int putBuffer(FIFO_QUEUE *fifo, int element)
{
printf("made it past initial check\n"); 
fifo->queue[12] = 23;
//insert the element at the next available position IFF there is one

if(printf("made it to putBuffer and fifo->queue[12] = %d\n", fifo->queue[12]) < 0)
    {
        printf("error in putBuffer\n");
        return -1;
}
//determine whether or not we need to "wrap" around to the beginning of the queue
if(fifo->putPos == fifo->size - 1)
    fifo->putPos = 0;  //wrap to the beginning
else
    fifo->putPos++;

//increment the number of items in the queue
fifo->numItems++;

//if all went well return 0
  return 0;

}

每個請求在這里是FIFO_QUEUE的定義,我在另一個函數中動態分配隊列結構,但是它存儲值並通過gdb打印

typedef struct fifoQueue{
int *queue;
int putPos;     //next position to insert to
int rmPos;      //next position to remove from
int numItems;   //number of items currently in the queue
int size;           //the max size of the queue
}FIFO_QUEUE;

這是我想出錯的地方,我需要在函數中動態分配fifo隊列,而我想做的是使用memcpy基本上創建一個fifo隊列,然后將其內容復制到共享內存中,但是似乎由於FIFO_QUEUE中的int *而導致斷開連接,我無法弄清楚。 我要去哪里了 我懷疑這與mkBuffer()函數中的動態分配有關,我的想法是memcpy只會復制其中的100字節,但是我可能會誤會

code for dynamic allocation

#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/types.h>
#include<stdlib.h>
#include<stdio.h>

#include <fcntl.h>           /* For O_* constants */
#include <sys/stat.h> 
#include<string.h>
#include "fifoQueue.h"


FIFO_QUEUE *makeBuffer(int size);


int main(int argc, char *argv[])
{
//our buffer
int i = 0;
int segment_id = 0;
FIFO_QUEUE *sharedBuff;
FIFO_QUEUE *fifo = NULL;

fifo = makeBuffer(25);

//-------retrieve COMMAND LINE arguments------//
if(argc != 2)
  {
    printf("getBuffer requires 2 command line args\n");
    exit(-1);
  }

//------SET UP SHARED MEMORY--------//

//get memory ID
segment_id = atoi(argv[1]);
printf("MAKE_BUFFER:  Shared mem seg Id in get buffer = %d\n", segment_id);

//Attach
sharedBuff = (FIFO_QUEUE*)shmat(segment_id, NULL, SHM_RND);

  //COPY contents of fifo into shared mem
memcpy((void*)sharedBuff, (void*)fifo, 120);


//--------CLEANUP--------//

//DETACH shared mem
shmdt(sharedBuff);

//deallocate memory
  rmBuffer(fifo);

  return 0;
}

/*  makeBuffer()
    Description:    
        - Creates a FIFO buffer of integers of size <size>
*/
FIFO_QUEUE *makeBuffer(int size)
{
//variables
int i = 0;
FIFO_QUEUE *fifo = NULL;

//allocate room for our struct
fifo = (FIFO_QUEUE*)malloc(sizeof(FIFO_QUEUE));

//allocate room for our queue
fifo->queue = (int*)malloc(sizeof(int) * size);

//set the initial position and number of items in the queue to 0
fifo->putPos = 0;
fifo->rmPos = 0;
fifo->numItems = 5;
fifo->size = size;
//return our pointer
return fifo;

}

這個:

memcpy((void*)sharedBuff, (void*)fifo, 120); /* Why 120 ? */

將從fifo復制120個字節:不會復制動態分配的fifo.queue數組。 由於queue所需的元素數是硬編碼的, FIFO_QUEUEFIFO_QUEUE的定義FIFO_QUEUE為:

typedef struct fifoQueue{
    int queue[25];
    int putPos;     //next position to insert to
    int rmPos;      //next position to remove from
    int numItems;   //number of items currently in the queue
    int size;       //the max size of the queue: always 25
}FIFO_QUEUE;

並將memcpy()更改為:

memcpy(sharedBuff, fifo, sizeof(*fifo));

暫無
暫無

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

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