为我的结构分配内存因段错误、未知地址而失败
Allocating memory for my struct failing with segfault, unknown address
很难给出简短的描述。
my_queue.h:
...
#define MAX_STRING_LENGTH 1025 // characters in each string
#define MAX_QUEUE_CAPACITY 20 // queue capacity
#define UNINITIALIZED -1000
typedef struct
{
char *name;
char (*data)[MAX_STRING_LENGTH]; // declare a pointer that
// can point to whole array
int head;
int tail;
int count;
int is_mt_safe;
sem_t items_available;
sem_t space_available;
sem_t mutex;
} queue;
...
这是我之前的做法:
my_queue.c:
...
queue *make_queue(char *name, int size, int mt_safe)
{
queue *new = malloc(sizeof(queue));
new->name = name;
new->is_mt_safe = mt_safe;
new->head = 0;
new->count = 0;
...
我根本没有初始化 queue->data
。相反,我刚开始写信给它
在 my_queue_push
:
strcpy(q->data[q->tail], str);
这个(或类似的东西,我必须回去检查)
曾经工作过,但我决定重新实施我的队列
因为我意识到我误解了我的要求,所以我想
队列根据需要增长而不是静态大小。
那时,我将队列->数据定义为不同的二维字符数组,
而不是指针。 (类似于 char data[MAX_QUEUE_CAPACITY][MAX_STRING_LENGTH]
)。作为使队列动态化的一部分,我尝试使用指向一组数组的指针而不是二维字符数组。
现在,当我尝试 运行 queue_push
中的行时,我看到了段错误。我认为这可能是一些不同的问题(各种数组初始化问题),但我使用了 AddressSanitizer 并且我认为我将它缩小到地址为 0x0000 ...,这似乎是我的 malloc'ing 队列的问题- >数据不正确。
AddressSanitizer:DEADLYSIGNAL
=================================================================
==245048==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x7fdec87b4e1f bp 0x7ffe5cab87b0 sp 0x7ffe5cab7f20
T0)
==245048==The signal is caused by a READ memory access.
==245048==Hint: address points to the zero page.
#0 0x7fdec87b4e1e (/lib/x86_64-linux-gnu/libasan.so.5+0x9be1e)
#1 0x55e94a06dcca in queue_push /home/brian/Workspace/MT-dns-resolver/mt-cirque.c:102
#2 0x55e94a06c895 in main /home/brian/Workspace/MT-dns-resolver/multi-lookup.c:28
#3 0x7fdec854e0b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
#4 0x55e94a06c6ad in _start (/home/brian/Workspace/MT-dns-resolver/multi-lookup+0x26ad)
我想过也许 malloc'ing queue->data 分开,但如果合适我不知道该怎么做。
我的代码可能有一些问题,因为我已经引导自己很快地编写了 C。显而易见的是 strcpy
可能不适用于指向 char 数组的指针?我在这个问题上来回走动,我看到人们遇到的问题与 SO 上的问题相似但不完全相同。
请多多包涵。如果您需要更多信息或者我犯了一个明显的错误,请告诉我,但是...
...您认为我在这里做错了什么?谢谢。
您还需要为数据分配 space,否则 data
指针将不会指向任何有用的地方:
queue *make_queue(char *name, int size, int mt_safe)
{
// Allocate memory for the structure itself
queue *new = malloc(sizeof *new);
// Allocate memory for the strings in the queue
// There will be `size` elements in the array of strings
new->data = malloc(size * sizeof *new->data);
...
}
此后q->data
可作为字符串数组使用
很难给出简短的描述。
my_queue.h:
...
#define MAX_STRING_LENGTH 1025 // characters in each string
#define MAX_QUEUE_CAPACITY 20 // queue capacity
#define UNINITIALIZED -1000
typedef struct
{
char *name;
char (*data)[MAX_STRING_LENGTH]; // declare a pointer that
// can point to whole array
int head;
int tail;
int count;
int is_mt_safe;
sem_t items_available;
sem_t space_available;
sem_t mutex;
} queue;
...
这是我之前的做法:
my_queue.c:
...
queue *make_queue(char *name, int size, int mt_safe)
{
queue *new = malloc(sizeof(queue));
new->name = name;
new->is_mt_safe = mt_safe;
new->head = 0;
new->count = 0;
...
我根本没有初始化 queue->data
。相反,我刚开始写信给它
在 my_queue_push
:
strcpy(q->data[q->tail], str);
这个(或类似的东西,我必须回去检查) 曾经工作过,但我决定重新实施我的队列 因为我意识到我误解了我的要求,所以我想 队列根据需要增长而不是静态大小。
那时,我将队列->数据定义为不同的二维字符数组,
而不是指针。 (类似于 char data[MAX_QUEUE_CAPACITY][MAX_STRING_LENGTH]
)。作为使队列动态化的一部分,我尝试使用指向一组数组的指针而不是二维字符数组。
现在,当我尝试 运行 queue_push
中的行时,我看到了段错误。我认为这可能是一些不同的问题(各种数组初始化问题),但我使用了 AddressSanitizer 并且我认为我将它缩小到地址为 0x0000 ...,这似乎是我的 malloc'ing 队列的问题- >数据不正确。
AddressSanitizer:DEADLYSIGNAL ================================================================= ==245048==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x7fdec87b4e1f bp 0x7ffe5cab87b0 sp 0x7ffe5cab7f20 T0) ==245048==The signal is caused by a READ memory access. ==245048==Hint: address points to the zero page. #0 0x7fdec87b4e1e (/lib/x86_64-linux-gnu/libasan.so.5+0x9be1e) #1 0x55e94a06dcca in queue_push /home/brian/Workspace/MT-dns-resolver/mt-cirque.c:102 #2 0x55e94a06c895 in main /home/brian/Workspace/MT-dns-resolver/multi-lookup.c:28 #3 0x7fdec854e0b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2) #4 0x55e94a06c6ad in _start (/home/brian/Workspace/MT-dns-resolver/multi-lookup+0x26ad)
我想过也许 malloc'ing queue->data 分开,但如果合适我不知道该怎么做。
我的代码可能有一些问题,因为我已经引导自己很快地编写了 C。显而易见的是 strcpy
可能不适用于指向 char 数组的指针?我在这个问题上来回走动,我看到人们遇到的问题与 SO 上的问题相似但不完全相同。
请多多包涵。如果您需要更多信息或者我犯了一个明显的错误,请告诉我,但是...
...您认为我在这里做错了什么?谢谢。
您还需要为数据分配 space,否则 data
指针将不会指向任何有用的地方:
queue *make_queue(char *name, int size, int mt_safe)
{
// Allocate memory for the structure itself
queue *new = malloc(sizeof *new);
// Allocate memory for the strings in the queue
// There will be `size` elements in the array of strings
new->data = malloc(size * sizeof *new->data);
...
}
此后q->data
可作为字符串数组使用