【问题标题】:Allocate space for struct pointer in subfunction为子函数中的结构指针分配空间
【发布时间】:2010-09-02 12:16:47
【问题描述】:

如何为结构指针分配内存并为其在子函数中的成员赋值?

以下代码将编译但不执行:

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

struct _struct {char *str;};
void allocate_and_initialize(struct _struct *s)
{
    s = calloc(sizeof(struct _struct), 1);
    s->str = calloc(sizeof(char), 12);
    strcpy(s->str, "hello world");
}
int main(void)
{
    struct _struct *s;
    allocate_and_initialize(s);
    printf("%s\n", s->str);

    return 0;
}

【问题讨论】:

    标签: c


    【解决方案1】:

    您正在按值传递s。调用allocate_and_initialize后,main中s的值不变

    要解决这个问题,您必须以某种方式确保 main 中的 s 指向函数分配的内存块。这可以通过将s 的地址传递给函数来完成:

    // s is now pointer to a pointer to struct.
    void allocate_and_initialize(struct _struct **s)
    {
            *s = calloc(sizeof(struct _struct), 1); 
            (*s)->str = calloc(sizeof(char), 12);
            strcpy((*s)->str, "hello world");                                                                                                                                                                      
    }
    int main(void)
    {
            struct _struct *s = NULL;  // good practice to make it null ptr.
            allocate_and_initialize(&s); // pass address of s.
            printf("%s\n", s->str);
    
            return 0;
    }
    

    或者,您可以返回函数中分配的块的地址,并将其分配给 main 中的s,如其他答案中所建议的那样。

    【讨论】:

    • 当你可以只返回一个值时,为什么还要添加更多的间接性?
    【解决方案2】:

    在你的例子中:

    void allocate_and_initialize(struct _struct *s)
    {
        s = calloc(sizeof(struct _struct), 1);
        s->str = calloc(sizeof(char), 12);
        strcpy(s->str, "hello world");
    }
    

    在此处分配给s 不会更改调用方中的s。为什么不退货呢?

    struct _struct *allocate_and_initialize(void) {
        struct _struct *s;
        s = calloc(sizeof *s, 1);
        s->str = calloc(1, 12); /* sizeof(char) is always 1 */
        strcpy(s->str, "hello world");
        return s;
    }
    

    并这样使用它:

    struct _struct *s;
    s = allocate_and_initialize();
    /* use s... */
    free(s); /* don't forget to free the memory when you're done */
    

    【讨论】:

    • 感谢您的建议,菲利普!在我的实际代码中,子函数分配了多个结构并返回分配的结构的数量。所以返回值不再可用。
    • @0penid 那么我会说你需要重新考虑你的设计。如果多个结构都是相关的,它们应该在一个大结构中。如果不是,它们不应该由一个大函数初始化,而是由单独的初始化函数初始化。
    【解决方案3】:

    您必须像这样更改您的代码:

        #include <stdio.h>
        #include <stdlib.h>
        #include <string.h>
    
        struct _struct {char *str;};
        void allocate_and_initialize(struct _struct **s)
        {
            *s = (_struct*)calloc(sizeof(struct _struct), 1);
            (*s)->str = (char*)calloc(sizeof(char), 12);
            strcpy((*s)->str, "hello world");
        }
        int main(void)
        {
            struct _struct *s;
            allocate_and_initialize(&s);
            printf("%s\n", s->str);
    
            return 0;
        }
    

    原因是,你改变了指针的地址,而不是指针的“内容”。因此,如果您在 c 中编码,则必须使用“双”指针。如果您使用 C++ 编写代码,则可以使用参考。

    【讨论】:

      【解决方案4】:

      您可以创建结构对象,然后将其地址传递给子函数,然后通过创建指针在子函数中赋值。确切的代码是,

      #include <stdio.h>
      #include <stdlib.h>
      #include <string.h>
      
      struct _struct {char *str;};
      
      void allocate_and_initialize(struct _struct *s)
      {
          s -> str = malloc(12);
          strcpy(s->str, "hello world");
      }
      
      void main(void)
      {
          struct _struct _struct;
          allocate_and_initialize(&_struct);
          printf("%s\n", _struct.str);
      }
      

      【讨论】:

        猜你喜欢
        • 2014-12-12
        • 1970-01-01
        • 2019-10-20
        • 2023-03-31
        • 2019-07-08
        • 2015-08-22
        • 2015-02-05
        • 2021-02-10
        • 2019-09-23
        相关资源
        最近更新 更多