我的C来源:
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
#include "xstring.h"
#include "queue.h"
struct queue* new_queue(size_t capacity, size_t data_size) {
struct queue* queue = xmalloc(sizeof(struct queue));
queue->capacity = capacity;
queue->data = xmalloc((capacity == 0 ? 1 : capacity) * data_size);
queue->rc = capacity == 0 ? 1 : 0;
queue->dsize = data_size;
依此类推,我的queue.h头文件定义了队列结构&amp;一些功能。
编译和链接工作正常,如果从命令行运行,则可以正常工作。但是,在Eclipse调试模式下运行会导致malloc返回指向无效地址的指针,并在queue->capacity = capacity;
行进行segfaulting。我试过在列表中移动标题,没有变化。我手动将标题内容粘贴到源文件的顶部,Eclipse停止显示错误,但仍然是segfaulted。我也试过重启,没有变化。
xmalloc以防万一:
void* xmalloc(size_t size) {
if (size > 10485760) {
printf("Big malloc %u!\n", size);
}
void* m = malloc(size);
if (m == NULL) {
printf("Out of Memory! @ malloc size %u\n", size);
exit(1);
}
return m;
}
以下是完整的queue.h
#ifndef QUEUE_H_
#define QUEUE_H_
#include <pthread.h>
struct queue {
size_t size;
size_t capacity;
size_t start;
size_t end;
size_t dsize;
size_t rc;
void* data;
pthread_mutex_t data_mutex;
pthread_mutex_t in_mutex;
pthread_cond_t in_cond;
pthread_mutex_t out_mutex;
pthread_cond_t out_cond;
};
struct queue* new_queue(size_t capacity, size_t data_size);
int del_queue(struct queue* queue);
int add_queue(struct queue* queue, void* data);
int pop_queue(struct queue* queue, void* data);
int peek_queue(struct queue* queue, void* data);
#endif /* QUEUE_H_ */