设置结构变量-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;//注意“->”:您正在取消对指针的引用。
示例->标题=“示例标题”;
返回示例;
}
谢谢这确实是问题所在。