C程序中结构的寿命

时间:2013-08-02 22:55:25

标签: c

假设我有以下结构:

typedef struct plane_t Plane;
struct plane_t{
    Point p1;
    Point p2;
    Point p3;
};

typedef struct arrangement_t* Arrangement;
struct arrangement_t{
    //TODO add fields here
    int maxPlanes;
    int curPlanes;
    Plane *planes;
};

我有以下功能:

Plane planeCreate(Point point1, Point point2, Point point3){

    Plane newPlane = {{point1.x, point1.y, point1.z}, {point2.x, point2.y, point2.z}, {point3.x, point3.y, point3.z}};
    return newPlane;
}

假设我正在编写一个函数,它将一个平面添加到arrangment_t结构中的数组平面中。

我可以执行以下操作:

arrangement->planes[arrangement->curPlanes] = planeCreate(plane.x, plane.y plane.z);

或者这个结构在退出这个函数后会“消失”,这意味着我必须遵循以下方式:

arrangement->planes[arrangement->curPlanes] = malloc(sizeof(struct plane_t));
    arrangement->planes[arrangement->curPlanes].x=plane.x;
    arrangement->planes[arrangement->curPlanes].x=plane.y;
    arrangement->planes[arrangement->curPlanes].x=plane.z; 

3 个答案:

答案 0 :(得分:8)

不,它不会消失。 C函数按值返回对象,因此结构将复制结束。

此外,

arrangement->planes[arrangement->curPlanes] = malloc(sizeof(struct plane_t));

甚至不会编译 - arrangement->planes[arrangement->curPlanes]不是指针而是结构。如果它是一个指针,那么只要你将赋值更改为

,你的代码就可以工作
arrangement->planes[arrangement->curPlanes]->x = plane.x;

(访问p指向的结构的成员是使用->运算符完成的,而不是.}


你可能正在谈论的不是返回局部变量本身,而是返回指针。例如,这个:

int *wrong_function()
{
    int answer = 42;
    return &answer;
}

是错误的 - 当函数返回时answer变量超出范围,并且其地址无效(因此上面将调用未定义的行为)。

答案 1 :(得分:4)

结构不会“消失”,因为你通过 value 返回它(实际上结构将被复制),而不是 reference (指针)。< / p>

答案 2 :(得分:0)

&#34;确定变量将保留多长时间是另一个困惑的问题 有抱负的程序员。让我们看一下关键字修饰符static。这个修饰符有 不幸的是,有几个目的是相关的。&#34; 使用static ....就像nlife {5}因为当静态用于函数或块中找到的变量时,它告诉编译器永远不要丢弃或重新分配变量。变量在编译时创建,并初始化为零。在这种情况下静态的反面是自动( 默认)。每次输入函数或块时,都会重新分配在函数或块内找到的变量。

/* LIFETIME, written 15 May 1992 by Peter D. Hipson */
/* An example of variable lifetime. */
#include <stdio.h> // Make includes first part of file
#include <string.h>
int nLife = {5}; // Initialize to 5, default is 0.
int main(void); // Define main() and the fact that this program doesn’t
// use any passed parameters.
void DisplayLife(void); // Define DisplayLife()
int main()
{
int nCounter = 0;
do
{
int nCountLoop = 0; /* This nCounter is unique to the loop */
nCountLoop += 3; /* Increments (and prints) the loop’s
nCounter */

nLife += nCounter;
printf(“nCountLoop is = %d\n”, nCountLoop);
}
while (++nCounter < 10); /* Increments the function’s nCounter */
DisplayLife();
printf(“Ended, nCounter is = %d\n”, nCounter);
return (0);
}
void DisplayLife()
{
printf(“DisplayLife(), nLife = %d?\n”, nLife);
}

在LIFETIME.C中,变量nLife对main()和DisplayLife()都是已知的。 这种变量的共享是一种可接受的编程实践,并且通常是这样 如前所述使用。 在前面的示例中,如果nLife的声明如下: static int nLife = {5}; //初始化为5,默认为零。 结果会是一样的。原因是该程序中只有一个源文件;因此,nLife必须只在一个文件中可见。只要有可能,请记住将外部变量设置为静态:如果只在一个源文件中知道它们,则它们不太可能被另一个源文件中的另一个函数无意地修改。

typedef struct plane_t Plane;
struct plane_t{
Point p1;
Point p2;
Point p3;
};

typedef struct arrangement_t* Arrangement;
struct arrangement_t{
//TODO add fields here
int maxPlanes;
int curPlanes;
Plane *planes;
};

也许因为void DisplayLife(void)与main(void)分开你可以在void DisplayLife()之外设置nlife + = nCounter并从平面和排列结构中传递值....嘿我可能是错误的,请打电话给我,我可以挨打=(