简体   繁体   English

在C中访问传递的struct数组的成员

[英]Accessing members of a passed struct array in C

I have a method levenshtein that populates a 2D array of struct sw/ info and returns a pointer to that array. 我有一个方法levenshtein ,它填充struct sw / info的2D数组并返回指向该数组的指针。 When I send it to another method, I get a Segmentation Fault (core dumped) error at runtime. 当我将其发送到另一种方法时,在运行时会出现Segmentation Fault (core dumped)错误。 Please help me w/ this hopefully obvious error. 请帮助我瓦特/这个希望明显的错误。

struct chartEntry
{
    int num;
    bool left,
         up,
         diag;
};

struct chartEntry** levenshtein(char *s1, char *s2, bool toPrint)
{
    unsigned int s1Len,
                 s2Len,
                 i, //rows, general purpose index
                 j; //columns, general purpose index
    s1Len = strlen(s1);
    s2Len = strlen(s2);

    /***********************************
    Create and populate traceback chart
    ***********************************/
    struct chartEntry chart [s1Len+1][s2Len+1];

    //
    // code to populate chart here
    //

    //prints expected number
    printf("chart[3][3].num is %d", chart[3][3].num);
    return chart;
}

void testFunction(char*s1,char*s2)
{
    // both of these give segmentation faults
    printf("[3][3].num is %d", levenshtein(s1,s2,false)[3][3].num);

    struct chartEntry ** tmp = levenshtein(s1,s2,false);
    printf("[3][3].num is %d", tmp[3][3].num);
}

There's 2 problems here. 这里有两个问题。 Firstly, chart is an array of arrays. 首先, chart是一个数组数组。 This cannot be converted to a pointer-to-a-pointer. 不能将其转换为指针指向的指针。 Arrays and pointers are different. 数组和指针是不同的。 Your line return chart; 您的折线return chart; must give you a compilation error which you should not ignore. 必须给您一个编译错误,您不应忽略该错误。

Secondly, even if it could be converted, chart 's memory is local to the levenshtein function and will no longer exist when that function returns. 其次,即使可以转换, chart的内存对于levenshtein函数也是本地的,并且在该函数返回时将不再存在。 So you would be returning a wild pointer. 因此,您将返回一个野指针。

You have two options: 您有两种选择:

  • allocate memory for chart using malloc and return a pointer to that (either one large block, or with two levels of indirection) 使用mallocchart分配内存,并返回指向该malloc的指针(一个大块或两个间接级别)
  • have the caller allocate the array and the levenshtein function just writes values into it. 让调用者分配数组,而levenshtein函数只是将值写入其中。

If you use the first option then you should not use the printf as you have done the first time in testFunction , because the memory would not be freed. 如果使用第一个选项,则不应像在testFunction第一次使用那样使用printf ,因为不会释放内存。 You have to save the returned pointer, printf it, and then execute a sequence of free that is the reverse of the malloc sequence you used to allocate it. 您必须保存返回的指针,对其进行printf,然后执行一个free序列,该序列与您用来分配它的malloc序列相反。

In your code, you're trying to return a pointer to an array that's declared inside the function: 在您的代码中,您试图返回一个指向函数内部声明的数组的指针:

struct chartEntry** levenshtein(char *s1, char *s2, bool toPrint)
{
    // ...

    struct chartEntry chart [s1Len+1][s2Len+1];

    // ...

    return chart;
}

However, as soon as the program exits this function, this array falls out of scope and is destroyed, leaving you with a pointer pointing to invalid memory. 但是,一旦程序退出此函数,此数组将超出范围并被破坏,从而留下指向无效内存的指针。 This leads to undefined behavior : Maybe it works, maybe it doesn't, maybe it puts your computer on fire. 这会导致未定义的行为 :也许它起作用,也许不起作用,也许使您的计算机着火。 Okay, that last one is pretty unlikely, but the point is, anything can happen . 好的,最后一个可能性很小,但是关键是, 任何事情都可能发生

There are two ways to deal with this problem: 有两种方法可以解决此问题:

  • Create a static array before calling the function, and then pass a pointer to this array in the function call. 调用函数之前创建一个静态数组,然后在函数调用中将指针传递给该数组。

  • Dynamically allocate memory in the function, which can then be returned as a normal pointer. 在函数中动态分配内存,然后可以将其作为普通指针返回。 (The calling function has to make sure the memory is freed after use, though, otherwise this may lead to memory leaks, which is a bad, bad thing.) (不过,调用函数必须确保在使用后释放内存,否则可能导致内存泄漏,这是一件很不好的事情。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM