gdb输出和终端输出之间的差异

时间:2012-03-04 21:56:20

标签: c debugging function gdb

我遇到了一个奇怪的问题。我正在为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的def我在另一个函数中动态分配队列结构但是它存储值并通过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;

}

1 个答案:

答案 0 :(得分:1)

此:

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

将从fifo复制120个字节:它不会复制动态分配的fifo.queue数组。由于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));