【问题标题】:Setting struct variables - C设置结构变量 - C
【发布时间】:2013-02-08 23:27:43
【问题描述】:

我在以下类型的布局中有一些代码,我相信当我调用 addTopBotExample 时,topExample/botExample 没有正确设置。我认为这是由于顶部 bot 变量位于函数堆栈上,因此在函数结束时被清除?我有一种感觉,也许我需要先malloc 内存,但我不确定我将如何去做这件事,即使它是正确的方法。

typedef struct Example Example;
struct Example {
   /* normal variables ...*/
   Example *topExample;
   Example *botExample;
};

....

void addTopBotExample(Example **example, int someVariable) {
    Example top = createTopExample(int someVariable); //(createTopExample returns a
                                                      //type Example based on some input)
    Example bot = createBotExample(int someVariable);
    (*example)->topExample = ⊤
    (*example)->botExample = ⊥
    return;
}

【问题讨论】:

    标签: c memory struct


    【解决方案1】:

    如果createTopExample 没有分配内存,那么当它被多次调用时就会出现问题。重写createTopExamplecreateBotExample 以使用malloc 并返回Example*。像这样的:

    Example* createTopExample(stuff)
    {
        Example *example = malloc(sizeof(Example)); 
        // ... stuff you do
        return example;
    }
    

    那么您的addTopBotExample 将如下所示:

     void addTopBotExample(Example **example, int someVariable) {
         if ((*example)->topExample)
             free((*example)->topExample)
         if ((*example)->botExample)
             free((*example)->botExample)
         (*example)->topExample = createTopExample(int someVariable);
         (*example)->botExample = createBotExample(int someVariable);
         return;
     }
    

    请注意,此addTopBotExample 将在再次调用malloc 之前free 分配的内存,但在您的程序结束之前,您需要在任何使用此addTopBotExample 函数的延迟Examples 上调用free

    free(exampleInstanceThatWasPassedIntoAddTopBotExampleAtSomePoint.topExample);
    free(exampleInstanceThatWasPassedIntoAddTopBotExampleAtSomePoint.botExample);
    

    【讨论】:

    • 谢谢,这确实是问题所在。
    【解决方案2】:

    你已经拥有了一切。在createTopExamplecreateTopExample 中分配Example

    Example *createTopExample(int someVariable)
    {
        Example *x = malloc(sizeof(Example));
        /* initialize x */
        return x;
    }
    

    addTopBotExample

    void addTopBotExample(Example *example, int someVariable) {
        Example *top = createTopExample(int someVariable); //(createTopExample returns a
                                                           //type Example based on some input)
        Example *bot = createBotExample(int someVariable);
        example->topExample = top;
        example->botExample = bot;
        return;
    }
    

    【讨论】:

      【解决方案3】:

      哎呀,这很糟糕。 addTopBotExample() 函数中的表达式“Example top”在堆栈上分配了该对象。退出该功能后将被丢弃。 (下一行的“示例机器人”也是如此。)这样的事情会更好:

      void addTopBotExample(Example **example, int someVariable) {
        Example *top = createTopExample(someVariable); // NOTE THE *
        Example *bot = createBotExample(someVariable); // NOTE THE *
      
        (*example)->topExample = top; // NOT &top !!
        (*example)->botExample = bot; // NOT &bot !!
        return;
      }
      

      并且您需要编写 createTopExample 和 createBotExample 以便它们返回指针:

      #include <stdlib.h> // For malloc!
      Example *createTopExample(stuff)  // Note *. It's returning a pointer.
      {
        Example *example = malloc(sizeof(Example)); // Allocate on the HEAP. Lives after this function call.
      
        // Fill in the fields of example.
        example->field1 = 25; // Note the "->": you're dereferencing a pointer.
        example->title = "Example title";
      
        return example;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-11-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-27
        相关资源
        最近更新 更多