【问题标题】:VS code problems suggestion in CC中的VS代码问题建议
【发布时间】:2021-04-06 17:44:57
【问题描述】:

我正在为学校编写 HW,我应该在其中实现 circular buffer,但我遇到了两件事。 VS Code 说:

  1. 函数调用中的参数太少 [8,21]
  2. 应为 ';' [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-&gt;arr_end =(int*)q-&gt;arr + capacity * sizeof(int);。如果arr_endint 类型,那么不,指针运算不能以这种方式工作。
  • @n.'pronouns'm。关于您的 q 指针未初始化是正确的,这几乎肯定会出现段错误,但这不是编译时错误,所以这不是导致您描述的错误的原因。请发布 GCC 日志。
  • @WoodrowBarlow 嗨,这是不知道的,可能是 malloc 或 sizeof。
  • 对于您的错误消息,请显示minimal reproducible example。这应该包含重现问题所需的所有代码,包括头文件和所有错误消息,未经编辑。

标签: c c99


【解决方案1】:

您忘记为队列预留空间:

queue_t * q = malloc(sizeof *q);

if (q != NULL)
{
    q->capacity = capacity;
    ...

还有

q->arr_end =(int*)q->arr + capacity * sizeof(int);

你想要的(假设你想要一个指向最后一个元素的指针):

q->arr_end = q->arr + capacity - 1;

指针运算是根据元素(不是字节)完成的

关于您的编译错误,您似乎忘记包含包含main的单元,请尝试

gcc main.c queue.c

【讨论】:

  • 不过,这不会是编译时错误,并且 mallocing sizeof 指针在这里不是正确的大小。它需要是sizeof(queue_t)(如果定义已知)
  • @WoodrowBarlow *qqueue_t 所以一切都很好。
  • @n.'pronouns'm。你能在自己的声明中取消引用一个指针吗?
  • @WoodrowBarlow sizeof 的参数没有被评估,没有取消引用发生。只有它的类型才需要它。是的,这是完全合法的。
  • @David Raniery,是的,我想用 q-&gt;arr_end 指向 arr 的末尾。谢谢。
猜你喜欢
  • 2021-08-26
  • 2022-12-18
  • 2021-11-14
  • 2020-12-05
  • 2022-11-16
  • 1970-01-01
  • 2011-02-21
  • 2018-12-02
  • 2021-08-11
相关资源
最近更新 更多