Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
设置结构变量-C_C_Memory_Struct - Fatal编程技术网

设置结构变量-C

设置结构变量-C,c,memory,struct,C,Memory,Struct,我在下面的布局中有一些代码,我认为在调用addTopBotExample时,topExample/botExample没有正确设置。我认为这是由于顶部的bot变量位于函数堆栈上,因此在函数结束时被清除?我有一种感觉,也许我需要先malloc内存,但我不确定如果这是正确的方法,我将如何进行 typedef struct Example Example; struct Example { /* normal variables ...*/ Example *topExample;

我在下面的布局中有一些代码,我认为在调用
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;
}

你已经把一切都准备好了。在
createTopExample
createTopExample

Example *createTopExample(int someVariable)
{
    Example *x = malloc(sizeof(Example));
    /* initialize x */
    return x;
}
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;
}
addTopBotExample

Example *createTopExample(int someVariable)
{
    Example *x = malloc(sizeof(Example));
    /* initialize x */
    return x;
}
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;
}

如果
createTopExample
没有分配内存,那么在多次调用时就会出现问题。重写
createTopExample
createBotExample
以使用
malloc
并返回
Example*
。大概是这样的:

Example* createTopExample(stuff)
{
    Example *example = malloc(sizeof(Example)); 
    // ... stuff you do
    return example;
}
 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
将如下所示:

Example* createTopExample(stuff)
{
    Example *example = malloc(sizeof(Example)); 
    // ... stuff you do
    return example;
}
 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;
 }
请注意,在再次调用
malloc
之前,此
addTopBotExample
free
释放分配的内存,但在程序结束之前,您需要对任何使用此
addTopBotExample
函数的延迟
Example
调用
free

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

这太糟糕了。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;
}
#包含//用于malloc!
示例*createTopExample(stuff)//注意*。它正在返回一个指针。
{
Example*Example=malloc(sizeof(Example));//在堆上分配。在此函数调用后生存。
//填写示例字段。
示例->字段1=25;//注意“->”:您正在取消对指针的引用。
示例->标题=“示例标题”;
返回示例;
}

谢谢这确实是问题所在。