malloc实验 [英] malloc experiments

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

问题描述

这个程序编译得很好,但是有没有任何隐藏的危险

,或者没关系?


实验1 ###### ############################################


#include< stdio.h>

#include< stdlib.h>

#include< malloc.h>

#include< string.h>


int main()

{

struct pres {

char name [25];

struct pres * next;

};


struct pres *总统;


总统=(struct pres *)malloc(sizeof(struct pres));


strcpy(总统 - >名称, George Washington);

president-> next =(struct pres *)malloc(sizeof(struct pres));


printf(" ;第一个结构已创建:\ n");

printf(" president-> name =%s \ n",president-> name);

printf(下一个结构地址=%i \ n,总统 - >下一个);


返回0;

}


#################### ################################################ >

--Steve

This program compiles fine, but are there any hidden dangers
in it, or is it ok?

Experiment 1 ##################################################

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>

int main()
{
struct pres {
char name[25];
struct pres *next;
};

struct pres *president;

president = (struct pres *)malloc(sizeof(struct pres));

strcpy(president->name, "George Washington");
president->next = (struct pres *)malloc(sizeof(struct pres));

printf("The first structure has been created:\n");
printf("president->name = %s\n", president->name);
printf("next structure address = %i\n", president->next);

return 0;
}

################################################## #################

--Steve

推荐答案

Steve Zimmerman< st ****** @ sonic达网络>写在

新闻:3F ************** @ sonic.net:
Steve Zimmerman <st******@sonic.net> wrote in
news:3F**************@sonic.net:
这个程序编译得很好,但是其中是否有任何隐患?或者它可以吗?

实验1 ####################### ###########################
#include< stdio.h>
#include< ; stdlib.h>
#include< malloc.h>
#include< string.h>

int main()
{
struct pres {
char name [25];
struct pres * next;
};

struct pres * president;

总裁=(struct pres *)malloc(sizeof(struct pres));


这是一个问题。如果你忽略了包含< stdlib.h>有没有
不是malloc()的原型,因此会返回一个整数。你的演员

会隐藏这个错误。在C(但不是C ++)中,我们不会转换malloc()的

返回值。其次,最好指定

对象的大小而不是其类型。这样,如果对象的类型发生变化,你就不会发现
必须找到malloc()调用并更改该行。例如


总统= malloc(sizeof *总统);


接下来,确保你被交给一个有效的指针,malloc()可以失败。如果它是
,也许你可以返回EXIT_FAILURE。

strcpy(president-> name,George Washington);


使用strncpy()并告诉strncpy不要超过sizeof size-> name - 1.

president-> next =(struct pres *) malloc(sizeof(struct pres));


(此处同样的malloc()评论)。

printf("第一个结构已创建:\ n");
printf(" president-> name =%s \ n",president-> name);
printf(" next structure address =%i \ n",president-> next) ;

返回0;
}
This program compiles fine, but are there any hidden dangers
in it, or is it ok?

Experiment 1 ##################################################

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>

int main()
{
struct pres {
char name[25];
struct pres *next;
};

struct pres *president;

president = (struct pres *)malloc(sizeof(struct pres));
This is a problem. If you had neglected to include <stdlib.h> there would
be no prototype for malloc(), thus it would return an integer. Your cast
would have hidden this mistake. In C (but not C++), we do not cast the
return value of malloc(). Second, it is best to specify the size of the
object not its type. That way, if the type of the object changes you don''t
have to find the malloc() call and change that line too. E.g.

president = malloc(sizeof *president);

Next, be sure you were handed a valid pointer, malloc() can fail. If it
does, maybe you could return EXIT_FAILURE.
strcpy(president->name, "George Washington");
Use strncpy() and tell strncpy not to exceed sizeof president->name - 1.
president->next = (struct pres *)malloc(sizeof(struct pres));
(Same malloc() comments here too).
printf("The first structure has been created:\n");
printf("president->name = %s\n", president->name);
printf("next structure address = %i\n", president->next);

return 0;
}




-

- 马克 - >

-



--
- Mark ->
--




" Steve Zimmerman" < ST ****** @ sonic.net>写在消息

新闻:3F ************** @ sonic.net ...

"Steve Zimmerman" <st******@sonic.net> wrote in message
news:3F**************@sonic.net...
这个程序编译好,但是其中有任何隐患
,还是没问题?

实验1 ######################## ##########################
#include< stdio.h>
#include< stdlib.h>
#include< malloc.h>
#include< string.h>


添加此定义以避免魔术数字

#define PRES_NAME_LEN 25

int main()
{
struct pres {
char name [25];
struct pres * next;
};


使用PRES_NAME_LEN代替魔法25.

struct pres * president;

总统=(struct pres *)malloc(sizeof (struct pres));
你必须检查malloc是否成功。

继续执行的条件是(总统!= NULL)

只是一个小问题;

只要stdlib.h包含

,你就不必从malloc转换结果(它应该是!)。

strcpy(总统 - >名称,乔治华盛顿);
糟糕。

如果字符串超过name的大小,会发生什么?结构中的字段?

有些东西会被破坏和覆盖!

使用

strncpy(president-> name)保护字段不被溢出,George Washington,PRES_NAME_LEN);

president-> next =(struct pres *)malloc(sizeof(struct pres));

printf("第一个结构已创建:\ n");
printf(" president-> name =%s \ n",president-> name);
printf(" next结构地址=%i \ n",president-> next);

返回0;
}

######### ################################################## ########

--Steve
This program compiles fine, but are there any hidden dangers
in it, or is it ok?

Experiment 1 ##################################################

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
Add this definition to avoid magic numbers
#define PRES_NAME_LEN 25


int main()
{
struct pres {
char name[25];
struct pres *next;
};
Use PRES_NAME_LEN instead of magic 25.

struct pres *president;

president = (struct pres *)malloc(sizeof(struct pres)); You have to check if malloc was successful.
The condition to continue execution is (president != NULL)
Just a minor remark;
You don''t have to cast the result from malloc as long as stdlib.h is
included (and it should be!).

strcpy(president->name, "George Washington"); Oops.
What happens if the string exceeds the size of "name" field in the struct?
Something will be destroyed and overwritten!
Protect the field from overflow by using
strncpy(president->name, "George Washington", PRES_NAME_LEN);
president->next = (struct pres *)malloc(sizeof(struct pres));

printf("The first structure has been created:\n");
printf("president->name = %s\n", president->name);
printf("next structure address = %i\n", president->next);

return 0;
}

################################################## #################

--Steve




最后,你必须释放你在此程序中分配的内存将

从原型转换为实际应用。


Mikael T



Finally, you must free the memory you''ve allocated when this program will
transform from prototype to a real application.

Mikael T


Steve Zimmerman写道:
Steve Zimmerman wrote:

这个程序com桩好了,但是里面有隐藏的危险吗?还是没问题?

实验1 ################### ###############################
#include< stdio.h>
#include< stdlib.h>
#include< malloc.h>


非标准标题。这是任何人猜测它可能会做什么。

#include< string.h>

int main()
{
struct pres {
char name [25];
struct pres * next;
};

struct pres * president;

president =(struct pres *)malloc(sizeof(struct pres));


两个小问题:演员阵容是不必要的,写'sizeof * president'会更好。

。一个主要的失态:malloc()

可能会失败并返回NULL,你应该在尝试使用可能未分配的内存之前检查这个。

strcpy(总统 - >名称,乔治华盛顿);
总统 - > next =(struct pres *)malloc(sizeof(struct pres));

printf("第一个结构已创建:\ n");
printf(" president-> name =%s \ n",president-> name);
printf(下一个结构地址=%i \ n,总统 - >下一个);


"%i"用于转换整数,而不是指针值。你需要使用%p,并且你需要将指针值

从`struct pres *'转换为'void *''。

返回0;
}

########################## #########################################

- -Steve

This program compiles fine, but are there any hidden dangers
in it, or is it ok?

Experiment 1 ##################################################

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
Non-Standard header. It''s anybody''s guess what it might do.
#include <string.h>

int main()
{
struct pres {
char name[25];
struct pres *next;
};

struct pres *president;

president = (struct pres *)malloc(sizeof(struct pres));
Two minor points: the cast is unnecessary, and it would be
better to write `sizeof *president''. One major gaffe: malloc()
can fail and return NULL, and you should check for this before
trying to use the possibly-not-allocated memory.
strcpy(president->name, "George Washington");
president->next = (struct pres *)malloc(sizeof(struct pres));

printf("The first structure has been created:\n");
printf("president->name = %s\n", president->name);
printf("next structure address = %i\n", president->next);
"%i" is for converting integers, not pointer values. You
need to use "%p", and you need to convert the pointer value
from a `struct pres*'' to a `void*''.
return 0;
}

################################################## #################

--Steve




-
Er ** *******@sun.com


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

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