寿命

2013-08-02 79 views
2

假设我有以下结构:寿命

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; 

回答

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变量超出范围的函数返回时,它的地址是无效的(因此上述将调用未定义行为)。

+0

感谢您的回答! –

+0

@Doppelganger不客气。 – 2013-08-03 04:39:04

4

结构不会“消失“,因为你通过返回它(实际上结构将被复制),而不是通过参考(指针)。

+1

正确。确切地说,本地对象'newPlane'确实消失,但'return'语句返回其值的副本。从int x = 42; return x;' –

+0

@KeithThompson原则上没有区别谢谢我已经添加了关于该副本的精确度。 – nouney

0

“确定可变的多长时间保持是困扰 抱负的程序员的另一个问题。让我们来看看关键字修改静态的。这个修正 ,不幸的是,有关几个目的。” 使用静态....就像nlife {5}因为当函数或块中的变量使用静态时,它告诉编译器永远不要放弃或重新分配变量。该变量在编译时创建并初始化为零。在这种情况下静态的相反是auto(默认为 )。每次输入函数或块时,都会重新分配在函数或块内部找到的变量。

/* 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; 
}; 

也许因为空隙DisplayLife(空隙)是从主(无效)单独你可以设置空隙DisplayLife(的nlife + =的nCounter外),并从平面和布置结构这种方式传递的值.. ..嘿,我可能错了打电话给我任何我可以采取殴打=(