gpt4 book ai didi

c - 将 void * 类型转换(或遵从)为 struct foo*

转载 作者:行者123 更新时间:2023-11-30 20:28:14 25 4
gpt4 key购买 nike

在 api.h 中

typedef void* hidden_my_type;
void do_something(my_type x);

在 core.c 中

struct _my_type
{
int a;
}

void do_something(hidden_my_type void_x)
{
struct *_my_type x = void_x; /*Don't understand is that correct way to do, as I'm getting segmentation fault error */
printf("Value: %d\n", x->a);
}

我认为的另一种方式,

struct *_my_type x = (struct _my_type *)malloc(sizeof(struct _my_type));
void_x = x
printf(Value: %d\n", x->a);

但我仍然遇到段错误错误。

<小时/>

好吧,这里是 void* 的问题......

例如在 core.c 中

void init_my_type(hidden_my_type a)
{
my_type *the_a = malloc(...);
a = the_a // <<<<<<<<<<<<<<<<<<<<<<<<<<<< is this correct?! a is void* and the_a // is original type
pthread_cond_init(&the_a->...);
.. (in short any other methods for init ..)
}
void my_type_destroy(my_hidden_type x)
{
my_type *the_x = x;
pthread_detroy(&the_x-> ...);
}

在main.c中

test()
{
my_hidden_type x;
init_my_type(x);
....
my_type_detroy(x);
}

这本身应该会失败。就像在 main.c 测试函数中一样,x 是 void* ... init 将分配,但在 destroy 中我再次传递 void* .. 可以是任何东西!

编辑(为我解决)

在 api.h 中

typedef void* hidden_my_type;
void do_something(my_type x);

在 core.c 中

 struct _my_type
{
int a;
}


void init_hidden_type(hidden_my_type void_p_my_type)
{
struct _my_type *real_my_type = (struct _my_type *)malloc(sizeof(struct _my_type));
//--- Do init for your type ---
void_p_my_type = real_my_type;
}


void do_something(hidden_my_type void_x)
{
struct *_my_type x = void_x;
printf("Value: %d\n", x->a);
}

最佳答案

版本 0 — 对问题代码的批评

发布的代码无法编译。

api.h

typedef void* hidden_my_type;
void do_something(my_type x);

这定义了 hidden_​​my_type,但没有定义传递给 do_something()my_type。想必您的意图是:

typedef void *my_type;
void do_something(my_type x);

核心.c

struct _my_type
{
int a;
}

也如下所述,结构定义后缺少一个分号。

void do_something(hidden_my_type void_x)
{
struct *_my_type x = void_x;
printf("Value: %d\n", x->a);
}

您又遇到了 hidden_​​my_typemy_type 问题。指针的 * 无法到达;它必须位于struct _my_type之后。您可能想要这样的东西:

void do_something(my_type void_x)
{
struct _my_type *x = void_x;
printf("Value: %d\n", x->a);
}

现在这在语法上是正确的(我认为;我实际上还没有通过编译器运行它)。您还没有展示它是如何使用的;事实上,由于用户代码无法生成指向有效结构的指针,因此无法安全地使用此代码。

您的测试代码(未显示 - 为什么不显示您的测试代码)可能如下所示:

#include "api.h"

int main(void)
{
my_type x = 0;
do_something(x);
return 0;
}

或者,它可能没有设置 = 0 初始值设定项。无论哪种方式,您的代码都无法正常运行,并且核心转储几乎是不可避免的。当您向用户隐藏结构时,您必须为他们提供一种机制来获取有效的结构(指向),但您还没有这样做。

版本 1

这是一种更好的方法,因为它更接近类型安全:

api.h 版本 1

typedef struct _my_type *my_type;
void do_something(my_type x);

core.c 版本 1

#include "api.h"
struct _my_type
{
int a;
};

请注意添加的分号以及 api.h 文件的包含内容。

void do_something(my_type x)
{
// Now you don't have to do casting here!
//struct *_my_type x = void_x; /*Don't understand is that correct way to do, as I'm getting segmentation fault error */
printf("Value: %d\n", x->a);
}
<小时/>

版本 2

实际上,我们可以讨论隐藏指针是否明智;我宁愿不这样做:

api.h 版本 2

#ifndef API_H_INCLUDED
#define API_H_INCLUDED

typedef struct my_type my_type;
extern void do_something(my_type *x);
extern my_type *my_type_initializer(void);
extern void my_type_release(my_type *x);

#endif /* API_H_INCLUDED */

core.c 版本 2

#include "api.h"
#include <stdio.h>
#include <stdlib.h>

struct my_type
{
int a;
};

void do_something(my_type *x)
{
printf("Value: %d\n", x->a);
}

my_type *my_type_initializer(void)
{
my_type *x = malloc(sizeof(*x));
x->a = 57; // More plausibly, this would be 0
return x;
}

void my_type_release(my_type *x)
{
free(x);
}

main.c

#include "api.h"

int main(void)
{
my_type *x = my_type_initializer();
do_something(x);
my_type_release(x);
return 0;
}

这又漂亮又干净。当然,用户无法分配struct my_type(只能指向它的指针),因此您需要一个函数来为它们分配结构。想想标准 C 库、FILE 类型、用于分配的 fopen() 和用于释放的 fclose() 以及 fprintf () 等来操作类型。 my_type_initializer() 的功能类似于 fopen()my_type_release() 类似于 fclose() code> 和 do_something()fprintf() 类似。

关于c - 将 void * 类型转换(或遵从)为 struct foo*,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10183499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com