【发布时间】: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;
}
【问题讨论】: