函数返回无效地址

huangapple go评论59阅读模式
英文:

Function returns invalid address

问题

UPD:好的,我很抱歉,但显然我提供的示例不足以重现我的错误,这就是为什么我将尝试提供更多关于我正在做的事情的信息(我再次道歉,我以为这是我遇到的同样的问题)。我正在开发用C语言编写的“库”,我想初始化这样的结构体

// some_head.h
typedef struct phony phony;

phony* create_phony();
int action_on_phony(phony* target);
// some_head.c
#include "some_head.h"

typedef struct phony {
  GLFWwindow* window;
  int w, h;
} phony;

据我所知,这种方式可以将结构体的实现隐藏起来,用户无法访问其中的数据,但我的库函数仍然可以使用它(可以说是一种实现封装的方式)。所以这就是为什么我有一个创建函数(构造函数)的原因:

// some_head.c
phony* create_phony()
{
    phony* created = malloc(sizeof(phony));
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
    created->w = 1920;
    created->h = 1080;
    created->window = glfwCreateWindow(1920/2, 1080/2, "Later", NULL, NULL);
    if (created->window)
      glfwMakeContextCurrent(created->window);
    return created;
}

int action_on_phony(phony* target)
{
      return !glfwWindowShouldClose(target->window); // <- raises BAD_ACCESS
}

这在some_head.c中运行良好,但是当我尝试在其他地方(例如main()函数)使用create_phony()时:

#include "some_head.h"

int main()
{
    phony* obj = create_phony(); // <- gets invalid address
    action_on_phony(obj); 
    return 0;
}

它给我返回了一个无效的地址,我无法将它传递给some_head.c中的其他函数,因为它们得到了无效的地址,当它们试图使用这个地址时会引发EXC_BAD_ACCESS。我猜这是因为phony结构体的实现未知(毕竟只有some_head.c知道它的大小),main函数不知道这是什么,就做出了这种事情。所以我的问题是,有没有办法正确地从some_head.c返回不完整的结构体,或者找到另一种封装phony结构体内部数据的方法?

英文:

UPD: Okay, I'm sorry, but apparently example I provided was not enough to reproduce my error, that is why I'll try to give more info on what I'm doing (I'm sorry again I thought it is same problem I have).
I'm developing "library" on c language and I want to initialize structure like this

// some_head.h
typedef struct phony phony;

phony* create_phony();
int action_on_phony(phony* target);
// some_head.c
#include &quot;some_head.h&quot;

typedef struct phony {
  GLFWwindow* window;
  int w, h;
} phony;

As I know, this way implementation of structure is hidden from user and he cannot access data in it, but still my library functions can work with it (some way of achieving encapsulation so to speak). So that's why I have some create function(constructor):

// some_head.c
phony* create_phony()
{
    phony* created = malloc(sizeof(phony));
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
    created-&gt;w = 1920;
    created-&gt;h = 1080;
    created-&gt;window = glfwCreateWindow(1920/2, 1080/2, &quot;Later&quot;, NULL, NULL);
    if (created-&gt;window)
      glfwMakeContextCurrent(created-&gt;window);
    return created;
}

int action_on_phony(phony* target)
{
      return !glfwWindowShouldClose(target-&gt;window); // &lt;- raises BAD_ACCESS
}

This works fine inside some_head.c, but when I'm trying to use this create_phony() thing somewhere else (ex. main()) like this:

#include &quot;some_head.h&quot;

int main()
{
    phony* obj = create_phony(); // &lt;- gets invalid address
    action_on_phony(obj); 
    return 0;
}

It returns invalid address to me and I cannot pass it to other functions in some_head.c since they get invalid address and raise EXC_BAD_ACCESS when they try to work with this address.
I assume this comes due unknown implementation of phony struct (only some_head.c knows size of it after all), that main does not know what is this and makes this kind of thing. So my question is is there a way to properly return incomplete structure from some_head.c or find another way to encapsulate data inside phony structure?

答案1

得分: 1

  1. 你的函数没有返回值。

这在 some_head.c 内部是可以正常工作的,但是当我尝试在其他地方使用这个 create_phony() 函数时就不行了。

  1. 这是因为结构体的 typedef 也应该在 .h 文件中。否则,你只有前向声明,但是编译器不知道这个结构体的大小。

你需要在调用这个函数的代码中包含它的声明(原型)在 .h 文件中。

phony *create_phony(void);

然后在一个 .c 文件中提供它的定义:

phony *create_phony(void)
{
    phony* obj = malloc(sizeof(*obj));
    if(obj)
    {  
        // 初始化
    }
    return obj;
}

注:

  1. sizeof 中使用对象而不是类型。
  2. 总是检查 malloc 系列函数的结果。
英文:
  1. Your function does not have a return value.

> This works fine inside some_head.c, but when I'm trying to use this
> create_phony() thing somewhere else

  1. It is because the typedef of the struct should be in the .h file as well. Otherwise you have only forward declaration but compiler does not have any clue what is the size of this structure.

You need to have its declaration (prototype) in the .h file which has to be included in the code which calls this function.

phony *create_phony(void);

And then in a .c file its definition:

phony *create_phony(void)
{
    phony* obj = malloc(sizeof(*obj));
    if(obj)
    {  
        // init
    }
    return obj;
}

Notes:

  1. use objects not types in sizeofs.
  2. always check the result of malloc family functions.

huangapple
  • 本文由 发表于 2023年3月9日 19:02:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75683708.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定