【发布时间】:2021-04-06 17:44:57
【问题描述】:
我正在为学校编写 HW,我应该在其中实现 circular buffer,但我遇到了两件事。 VS Code 说:
- 函数调用中的参数太少 [8,21]
- 应为 ';' [9,5]
但我很确定,到目前为止我没有犯任何错误。我也不知道如何编译它,GCC不会接受。学校提供的Makefile 抛出了一些错误,但没有关于这个问题。
我有来自 Microsoft [v1.2.2] 的 C/C++ 扩展。错误/问题是否由该人处理?
这里是代码queue.c:
#include "queue.h"
// TODO - your code
queue_t* create_queue(int capacity){
queue_t * q;
q->capacity = capacity;
q->count = 0;
q->arr = malloc(capacity*sizeof(int));
if(q->arr == NULL){
fprintf(stderr, "ERROR: cannot alocate enough memory!\n"); // here is the er#1
}
q->arr_end =(int*)q->arr + capacity * sizeof(int);
return q; // er#2 occurs here
}
这里是 queue.h
#ifndef __QUEUE_H__
#define __QUEUE_H__
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
/* Queue structure which holds all necessary data */
typedef struct {
// TODO - Include your data structure here
int capacity; // the max # of elemetns, that can be stored
int count; // # of elements in Q
int * arr; // the array itself
int * arr_end; // pointer to the end of arr (ie: *(arr+int*len))
int * read; // position to read from; ie: HEAD
int * write; // position to write form; ie: TAIL
} queue_t;
/* creates a new queue with a given size */
queue_t* create_queue(int capacity);
// ...
#endif /* __QUEUE_H__ */
gcc queue.c 的 GCC 输出
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x24): undefined reference to `main'
collect2: error: ld returned 1 exit status
这是来自学校的main.c:
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "queue.h"
/* allocate new integer with value a and add it to the queue */
void add(int a, queue_t *queue)
{
int *p = (int*)malloc(sizeof(int));
*p = a;
bool ret = push_to_queue(queue, (void*)p);
if (!ret) {
// free memory on failure
free(p);
}
}
/* print the int value on pointer p */
void print_int(void *p)
{
if (p != NULL) {
printf("%d\n", *((int*)p));
} else {
printf("NULL\n");
}
}
/* pop from the queue, print and free the element */
void pop(queue_t *queue)
{
void *p = pop_from_queue(queue);
print_int(p);
free(p);
}
/* get i-th element and print it (do not remove them) */
void get(int idx, queue_t *queue)
{
print_int(get_from_queue(queue, idx));
}
/*
* TEST PROGRAM
* - reads commands from stdin and executes them in the queue
*/
int main(int argc, char *argv[])
{
int n;
/* the tested queue */
queue_t *queue;
// read the size of the queue
scanf("%d", &n);
// create queue
queue = create_queue(n);
while (true) {
char s[2];
// read one command
int ret = scanf("%1s", s);
if (ret != 1) {
break;
}
// add command
if (s[0] == 'a') {
int a;
// read the argument of the command
ret = scanf("%d", &a);
if (ret != 1) {
break;
}
add(a, queue);
// remove command
} else if (s[0] == 'r') {
pop(queue);
// get command
} else if (s[0] == 'g') {
int a;
// read the argument of the command
ret = scanf("%d", &a);
if (ret != 1) {
break;
}
get(a, queue);
}
}
// remove rest of the elements in the queue
while (get_queue_size(queue)) {
void *p = pop_from_queue(queue);
free(p);
}
// free memory
delete_queue(queue);
queue = NULL;
// return 0 on succes
return 0;
}
【问题讨论】:
-
第 8 行是 malloc 吗?
-
q应该指向一个queue_t对象,但是您没有初始化它并且它指向任何地方。在 C 语言中,这是未定义的行为,您需要分配一个queue_t,大概是调用malloc,并将其地址分配给q。另一个很可能的错误来源是q->arr_end =(int*)q->arr + capacity * sizeof(int);。如果arr_end是int类型,那么不,指针运算不能以这种方式工作。 -
@n.'pronouns'm。关于您的
q指针未初始化是正确的,这几乎肯定会出现段错误,但这不是编译时错误,所以这不是导致您描述的错误的原因。请发布 GCC 日志。 -
@WoodrowBarlow 嗨,这是不知道的,可能是 malloc 或 sizeof。
-
对于您的错误消息,请显示minimal reproducible example。这应该包含重现问题所需的所有代码,包括头文件和所有错误消息,未经编辑。