使用灵活长度数组时的 Objective-C EXEC_BAD_ACCESS

发布于 2024-10-17 12:41:25 字数 960 浏览 8 评论 0原文

我对我发现的一些行为做了一些测试,我想知道是否有人可以帮助我了解发生了什么。

我有一个名为 myStruct 的结构,如下所示:

typedef struct {
    int size;
    float floats[];
} myStruct;

我在其上运行此代码:

int main () {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSArray *a = [[NSArray alloc] initWithObjects:@"0.2", @"0.5", @"0.5", nil];

    NSLog(@"%@", a);

    myStruct my;
    my.size = a.count;

    my.floats[0] = [[a objectAtIndex:0] floatValue];
    my.floats[1] = [[a objectAtIndex:1] floatValue];
    my.floats[2] = [[a objectAtIndex:2] floatValue];

    NSLog(@"{ %lf, %lf, %lf }", my.floats[0], my.floats[1], my.floats[2]);

    [a release];    
    [pool drain];
    return 0;
}

它工作正常。但是,当我将结构声明更改为:

typedef struct {
    float myVar;
    int size;
    float floats[];
} myStruct;

当我调用行 [a release] 时,我得到 EXEC_BAD_ACCESS。

谁能帮我理解这里发生了什么?

I have done some testing on some behavior I have found, and I was wondering if someone can help me understand what is going on.

I have a struct, called myStruct, that looks like this:

typedef struct {
    int size;
    float floats[];
} myStruct;

And I run this code on it:

int main () {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSArray *a = [[NSArray alloc] initWithObjects:@"0.2", @"0.5", @"0.5", nil];

    NSLog(@"%@", a);

    myStruct my;
    my.size = a.count;

    my.floats[0] = [[a objectAtIndex:0] floatValue];
    my.floats[1] = [[a objectAtIndex:1] floatValue];
    my.floats[2] = [[a objectAtIndex:2] floatValue];

    NSLog(@"{ %lf, %lf, %lf }", my.floats[0], my.floats[1], my.floats[2]);

    [a release];    
    [pool drain];
    return 0;
}

It works fine. However, when I change the struct declaration to this:

typedef struct {
    float myVar;
    int size;
    float floats[];
} myStruct;

I get EXEC_BAD_ACCESS when I call the line [a release].

Can anyone help me understand what is going on here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幸福%小乖 2024-10-24 12:41:25

您必须实际为灵活的数组成员分配空间!此行:

myStruct my;

仅为 size (或第二个示例中的 myVarsize)提供足够的堆栈空间。看起来,在失败的情况下,您正在覆盖堆栈上的 a ,但实际上这两种情况都是错误的。要解决您的问题,您需要为结构的 floats[] 成员分配空间:

myStruct *my = malloc(sizeof(myStruct) + a.count * sizeof(float));

完成后不要忘记 free() 它!

一个简单的例子 - 这个程序:

#include <stdio.h>

typedef struct {
    float myVar;
    int size;
    float floats[];
} myStruct;

int main(int argc, char **argv)
{
  printf("%zu\n", sizeof(myStruct));
  return 0;
}

及其输出:

$ make testapp
cc     testapp.c   -o testapp
$ ./testapp 
8

You have to actually allocate space for your flexible array member! This line:

myStruct my;

Only makes enough stack space for size (or myVar and size from your second example). It appears that in your failing case you're overwriting a on the stack, but really both cases are wrong. To fix your problem, you need to allocate space for the floats[] member of the structure:

myStruct *my = malloc(sizeof(myStruct) + a.count * sizeof(float));

Don't forget to free() it when you're done!

A quick example - this program:

#include <stdio.h>

typedef struct {
    float myVar;
    int size;
    float floats[];
} myStruct;

int main(int argc, char **argv)
{
  printf("%zu\n", sizeof(myStruct));
  return 0;
}

and its output:

$ make testapp
cc     testapp.c   -o testapp
$ ./testapp 
8
九厘米的零° 2024-10-24 12:41:25

你没有为你的浮动分配任何内存 - 我很惊讶它没有更快崩溃!

您需要为 floats[] 指针分配一些内存吗?


经过快速测试后,我得到了 myStruct 的两个定义的 EXC_BAD_ACCESS :)

You're not allocating any memory for your floats - I'm surprised it's not crashing sooner!

Do you need to malloc some memory for the floats[] pointer?


After a quick test I get EXC_BAD_ACCESS for both definitions of myStruct :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文