【问题标题】:malloc of a structure in C ( ported from C++ )C 中结构的 malloc(从 C++ 移植)
【发布时间】:2019-07-30 09:37:57
【问题描述】:

我想在堆上保留一些内存空间并用指针访问它。

代码在 C++ 中运行良好,但我无法在 C 中编译。

#include <string.h>
#include <stdlib.h>

#define IMG_WIDTH 320

struct cluster_s
{
  uint16_t size;
  uint16_t xMin;
  uint16_t xMax;
  uint16_t yMin;
  uint16_t yMax;
};

static struct cluster_s* detectPills(const uint16_t newPixel[])
{

  static struct cluster_s **pixel = NULL;
  static struct cluster_s *cluster = NULL;

  if(!pixel){
    pixel = (cluster_s**) malloc(IMG_WIDTH * sizeof(struct cluster_s*));
    if(pixel == NULL){
      return NULL;
    }
  }
  if(!cluster){
    cluster = (cluster*) malloc((IMG_WIDTH+1) * sizeof(struct cluster_s));
    if(cluster == NULL){
      return NULL;
    }
    for(int i=0; i<IMG_WIDTH;i++){
      memset(&cluster[i], 0, sizeof(cluster[i]));
      pixel[i] = &cluster[i];
    }
  }
(...)
}

这给了我以下编译错误:

error: 'cluster_s' undeclared (第一次在这个函数中使用) 像素 = (cluster_s**) malloc(IMG_WIDTH * sizeof(struct *cluster_s));

如果我注释掉两个 malloc 调用,我就可以编译它。 我还尝试在 malloc 之前删除强制转换并得到编译错误:

在函数_sbrk_r': sbrkr.c:(.text._sbrk_r+0xc): undefined reference to_sbrk' collect2:错误:ld 返回 1 个退出状态

编辑: 建议的答案是正确的,问题来自找不到 sbrk 的链接器

【问题讨论】:

  • (cluster_s*) ==> (struct cluster_s*) 但在 C 语言中,无论如何你都不应该需要演员表。如果您在删除强制转换后出现编译错误,那么它不是 C 编译器,可能文件是 .cpp。
  • 添加 (struct cluster_s*) 给了我同样的错误,我没有强制转换。即:在函数_sbrk_r'中:sbrkr.c:(.text._sbrk_r+0xc): undefined reference to_sbrk' collect2: error: ld returned 1 exit status
  • 您也必须在另一行更改(cluster_s**)
  • 是的,我也这样做了。

标签: c struct casting malloc typedef


【解决方案1】:

这个

我还尝试在 malloc 之前删除演员表并获得编译 错误:

还有这个

代码在 C++ 中运行良好,但我无法在 C 中编译。

互相矛盾。

第一个表示您正在尝试将程序编译为 C++ 程序。

要使程序编译为 C++ 程序和 C 程序,有两种方法。

第一个在程序中到处都是使用类型说明符 struct cluster_s 而不仅仅是 cluster_s 。例如

pixel = (struct cluster_s**) malloc(IMG_WIDTH * sizeof(struct cluster_s*));
         ^^^^^^^^^^^^^^^^  
//...
cluster = (struct cluster*) malloc((IMG_WIDTH+1) * sizeof(struct cluster_s));
           ^^^^^^^^^^^^^^

第二个是为类型说明符struct cluster_slike引入一个别名

typedef struct cluster_s cluster_s;

【讨论】:

  • 两种可能性都给我编译错误:在函数_sbrk_r': sbrkr.c:(.text._sbrk_r+0xc): undefined reference to _sbrk'collect2:错误:ld返回1退出状态
  • @FlorianK 撇号是干什么用的?函数_sbrk_r'在哪里?
  • @FlorianK 你问的问题没有什么共同点。未定义的错误意味着编译器没有看到 _sbrk (或 sbrk )的定义
  • @WeatherVane @Vlad 来自莫斯科 完整的错误是:Compiling camera.c Linking build/ch.elf /home/flo/bin/gcc-arm-none-eabi-7-2018-q2-更新/bin/../lib/gcc/arm-none-eabi/7.3.1/../../../../arm-none-eabi/lib/thumb/v7e-m/fpv4-sp /hard/libg.a(lib_a-sbrkr.o):在函数_sbrk_r': sbrkr.c:(.text._sbrk_r+0xc): undefined reference to _sbrk'collect2:错误:ld 返回 1 退出状态 lib/ChibiOS/os/common/startup/ARMCMx/compilers/GCC/mk/rules。 mk:204: 目标 'build/ch.elf' 的配方失败 make: *** [build/ch.elf] 错误 1
  • error: ld 不是来自 链接器 吗?这意味着没有编译错误。
【解决方案2】:

替换

struct cluster_s
{
   ...
};

typedef struct cluster_s 
{
    ....
}cluster_s;

在 C++ 中,结构和类是类似的,大部分情况下可以互换使用。所以cluster_sstruct cluster_s都可以使用。

在 C 中,cluster_s 未定义。上面的更改将定义一个同名的类型。

类和结构的区别可以看答案When should you use a class vs a struct in C++?

【讨论】:

  • 我想我应该以不同的方式表达我的问题,在使用任何正确建议的答案正确调用 malloc 后,我总是得到同样的奇怪错误。错误是:In function _sbrk_r': sbrkr.c:(.text._sbrk_r+0xc): undefined reference to _sbrk' collect2: error: ld returned 1 exit status
  • 您可以通过stackoverflow.com/questions/28895703/…。可能会解决_srbk 问题
猜你喜欢
  • 1970-01-01
  • 2012-10-02
  • 1970-01-01
  • 1970-01-01
  • 2021-12-08
  • 2012-01-19
  • 2011-02-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多