结构和malloc [英] structure and malloc

查看:65
本文介绍了结构和malloc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

typedef struct

{

int a;

int b;

int c;

} ABC;


typedef struct

{

int d;

int e;

int f;

ABC * abc;

} DEF;


创建函数应该使用对malloc的单个调用来为结构DEF及其构成结构ABC分配

内存。在其他

字样中,在调用CreateDEF()函数后,以下

语句应该有效,并且应该在没有分段的情况下工作

违规:


DEF * pDEF = CreateDEF();

pDEF - > abc - > a = 100;

DEF * CreateDEF(无效)

{

}


无效FreeDEF(ABC * pABC)

{

}

typedef struct
{
int a;
int b;
int c;
} ABC;

typedef struct
{
int d;
int e;
int f;
ABC *abc;
} DEF;

Create function should use a single call to malloc to allocate
memoryfor structure DEF and its constituting structure ABC. In other
words, after a call to the CreateDEF() function, the following
statements should be valid and should work without segmentation
violations:

DEF* pDEF = CreateDEF();
pDEF-->abc-->a = 100 ;
DEF *CreateDEF ( void )
{
}

void FreeDEF (ABC *pABC)
{
}

推荐答案

friend.05写道:
friend.05 wrote:

typedef struct

{

int a;

int b;

int c;

} ABC;


typedef struct

{

int d;

int e;

int f;

ABC * abc;

} DEF;


创建函数应该使用对malloc的单个调用来为结构DEF及其构成结构ABC分配

内存。在其他

字样中,在调用CreateDEF()函数后,以下

语句应该有效,并且应该在没有分段的情况下工作

违规:


DEF * pDEF = CreateDEF();

pDEF - > abc - > a = 100;


DEF * CreateDEF(无效)

{

}


无效FreeDEF(ABC * pABC)

{

}
typedef struct
{
int a;
int b;
int c;
} ABC;

typedef struct
{
int d;
int e;
int f;
ABC *abc;
} DEF;

Create function should use a single call to malloc to allocate
memoryfor structure DEF and its constituting structure ABC. In other
words, after a call to the CreateDEF() function, the following
statements should be valid and should work without segmentation
violations:

DEF* pDEF = CreateDEF();
pDEF-->abc-->a = 100 ;
DEF *CreateDEF ( void )
{
}

void FreeDEF (ABC *pABC)
{
}



在一个完整的程序中尽可能地抓住它。然后我们会帮忙。我们

不做作业。


Brian


Take your best stab at it, in a complete program. Then we''ll help. We
don''t do homework.


Brian


typedef struct

{

int a;

int b;

int c;

} ABC;


typedef struct

{

int d;

int e;

int f;

ABC * abc;

} DEF;


DEF * CreateDEF(void)

{

DEF strDEF;


strDEF.abc =(ABC *)malloc(sizeof(ABC));

strDEF.abc-> a = 10;

printf("%d \ n",strDEF.abc-> a);

retDEF =& strDEF;

返回retDEF;

}


void FreeDEF(ABC * pABC)

{


}

void main()

{

DEF * pdef = CreateDEF();

pdef-> d = 7;

printf("%d \ n",pdef - > d);

pdef-> abc-> a = 10;

printf("%d \ n",pde f-> abc-> a);

}

但它给出了分段错误



在5月8日下午5:28,默认用户 < defaultuse ... @ yahoo.comwrote:
typedef struct
{
int a;
int b;
int c;
} ABC;

typedef struct
{
int d;
int e;
int f;
ABC *abc;
} DEF;

DEF *CreateDEF (void)
{
DEF strDEF;

strDEF.abc=(ABC *)malloc(sizeof(ABC));
strDEF.abc->a = 10;
printf("%d\n",strDEF.abc->a);
retDEF=&strDEF;
return retDEF;
}

void FreeDEF (ABC *pABC)
{


}

void main()
{
DEF *pdef = CreateDEF();
pdef->d = 7;
printf("%d\n",pdef->d);
pdef->abc->a = 10;
printf("%d\n",pdef->abc->a);
}
But it is giving segmentation fault


On May 8, 5:28 pm, "Default User" <defaultuse...@yahoo.comwrote:

friend.05写道:
friend.05 wrote:

typedef struct

{

int a;

int b;

int c;

ABC;
typedef struct
{
int a;
int b;
int c;
} ABC;


typedef struct

{

int d;

int e;

int f;

ABC * abc;

} DEF;
typedef struct
{
int d;
int e;
int f;
ABC *abc;
} DEF;


创建函数应该使用对malloc的单个调用来为结构DEF及其构成结构ABC分配

内存。在其他

字样中,在调用CreateDEF()函数后,以下

语句应该有效,并且应该在没有分段的情况下工作

违规:
Create function should use a single call to malloc to allocate
memoryfor structure DEF and its constituting structure ABC. In other
words, after a call to the CreateDEF() function, the following
statements should be valid and should work without segmentation
violations:


DEF * pDEF = CreateDEF();

pDEF - > abc - > a = 100 ;
DEF* pDEF = CreateDEF();
pDEF-->abc-->a = 100 ;


DEF * CreateDEF(无效)

{

}
DEF *CreateDEF ( void )
{
}


void FreeDEF(ABC * pABC)

{

}
void FreeDEF (ABC *pABC)
{
}



在一个完整的程序中尽可能地抓住它。然后我们会帮忙。我们

不做作业。


Brian-隐藏引用的文字 -


- 显示引用的文字 -


Take your best stab at it, in a complete program. Then we''ll help. We
don''t do homework.

Brian- Hide quoted text -

- Show quoted text -



文章< 11 ********************* *@y5g2000hsa.googlegroups。 com>,

friend.05< hi ********** @ gmail.comwrote:
In article <11**********************@y5g2000hsa.googlegroups. com>,
friend.05 <hi**********@gmail.comwrote:

> DEF * CreateDEF(无效)
$

DEF strDEF;


strDEF.abc =(ABC *)malloc(sizeof(ABC));

strDEF.abc-> a = 10;

printf("%d \ nn",strDEF.abc-> a);

retDEF =& strDEF;

返回retDEF;
}
>DEF *CreateDEF (void)
{
DEF strDEF;

strDEF.abc=(ABC *)malloc(sizeof(ABC));
strDEF.abc->a = 10;
printf("%d\n",strDEF.abc->a);
retDEF=&strDEF;
return retDEF;
}



你不能返回指针到局部变量(DEF)。 (或者更确切地说,

一旦它返回
你就不能用这样的指针做任何事情。)


考虑分配一个足够大的内存区域来支持两个结构。

-

法律 - 它是一种商品

- Andrew Ryan(环球邮报,2005/11/26)

You can''t return a pointer to a local variable (DEF). (Or rather,
you can''t do anything with such a pointer once it has been
returned.)

Consider allocating a memory area big enough to hold both
structures.
--
"law -- it''s a commodity"
-- Andrew Ryan (The Globe and Mail, 2005/11/26)


这篇关于结构和malloc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆