假设我在ANSI C中有这个结构:
typedef struct _point
{
float x;
float y;
} Point;
而这个函数来创建这个struct:
Point createpoint(float x, float y)
{
Point p;
p.x = x;
p.y = y;
return p;
}
这允许我创建一个具有此功能的结构体,即:
int main()
{
Point pointOne = createpoint(5, 6);
Point pointTwo = createpoint(10, 4);
float distance = calculatedistancefunc(pointOne, pointTwo);
/* ...other stuff */
return 0;
}
现在我的问题有人告诉我,这段代码是无效的,因为在返回结果之前,它不会在de createpoint(float x,float y)函数中获取malloc’d,并且结构将被删除.
但是,当我使用这样的结构时,似乎没有被删除.
所以我的问题是:
我必须malloc这个结构,为什么? / 为什么不?