想要优化这个程序,有什么建议吗? [英] Want to optimize this procedure, any advice?

查看:49
本文介绍了想要优化这个程序,有什么建议吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还在学习C所以我写了一个简单的应用程序,它可以让你创建一个

联系人列表(存储为结构的链接列表),将其写入文件,并且

读回来。它工作正常,但我注意到在我的加载过程中有一两个点或两个点我将malloc一个我不需要的结构,并且

处置它;我想知道是否有某种方法可以优化这个

kludge的存在:


void loadContacts(){int fd,bytesread; if((fd = open(" contacts.data",O_RDONLY,0))!= -1){struct contact * prev,* curr =(struct contact *)malloc(sizeof(struct contact)); if(curr){if((bytesread = read(fd,curr,sizeof(struct contact)))== sizeof(struct contact)){first.next = curr; //首先是一个全局变量,它是链表中的第一个结构} else {free(curr); //我可以避免这样做吗? } do {prev = curr; curr =(struct contact *)malloc(sizeof(struct contact)); prev-> next = curr; while((bytesread = read(fd,curr,sizeof(struct contact)))== sizeof(struct contact)); if(bytesread< sizeof(struct contact)){prev-> next = NULL;自由(CURR); //同样,这个? } else {perror(" malloc. \ n");出口(1); } close(fd); } $

-
http://www.metawire。组织 - 开放主机


今天获得免费外壳和

加入一个不断增长的用户社区!

I''m still learning C so I''ve written a simple app which lets you make a
contact list (stored as a linked list of structs), write it to a file, and
read it back. It works fine, but I notice in my loading procedure there''s
a point or two where I''ll malloc a struct which I then don''t need, and
dispose of it; I was wondering if there was some way I could optimize this
kludge out of existence:

void loadContacts () { int fd, bytesread; if ( (fd = open("contacts.data", O_RDONLY, 0) ) != -1 ) { struct contact *prev, *curr = (struct contact *)malloc(sizeof(struct contact)); if ( curr ) { if ( ( bytesread = read(fd, curr, sizeof(struct contact))) == sizeof(struct contact) ) { first.next = curr; // First is a global variable which is the first struct in the linked list } else { free (curr); // Can I avoid having to do this? } do { prev = curr; curr = (struct contact *)malloc(sizeof(struct contact)); prev->next = curr; } while ( ( bytesread = read(fd, curr, sizeof(struct contact))) == sizeof(struct contact) ); if ( bytesread < sizeof(struct contact) ) { prev->next = NULL; free(curr); // Likewise, this? } } else { perror("malloc.\n"); exit(1); } close (fd); }}
--
http://www.metawire.org - Open Hosting

Get your free shell today and
join a growing community of users!

推荐答案

" Michael B." <我们***** @ spamblocked.com>写道:
"Michael B." <us*****@spamblocked.com> writes:
void loadContacts(){int fd,bytesread; if((fd = open(" contacts.data",O_RDONLY,0))!= -1){struct contact * prev,* curr =(struct contact *)malloc(sizeof(struct contact)); if(curr){if((bytesread = read(fd,curr,sizeof(struct contact)))== sizeof(struct contact)){first.next = curr; //首先是一个全局变量,它是链表中的第一个结构} else {free(curr); //我可以避免这样做吗? } do {prev = curr; curr =(struct contact *)malloc(sizeof(struct contact)); prev-> next = curr; while((bytesread = read(fd,curr,sizeof(struct contact)))== sizeof(struct contact)); if(bytesread< sizeof(struct contact)){prev-> next = NULL;自由(CURR); //同样,这个? } else {perror(" malloc. \ n");出口(1); } close(fd); }}
void loadContacts () { int fd, bytesread; if ( (fd = open("contacts.data", O_RDONLY, 0) ) != -1 ) { struct contact *prev, *curr = (struct contact *)malloc(sizeof(struct contact)); if ( curr ) { if ( ( bytesread = read(fd, curr, sizeof(struct contact))) == sizeof(struct contact) ) { first.next = curr; // First is a global variable which is the first struct in the linked list } else { free (curr); // Can I avoid having to do this? } do { prev = curr; curr = (struct contact *)malloc(sizeof(struct contact)); prev->next = curr; } while ( ( bytesread = read(fd, curr, sizeof(struct contact))) == sizeof(struct contact) ); if ( bytesread < sizeof(struct contact) ) { prev->next = NULL; free(curr); // Likewise, this? } } else { perror("malloc.\n"); exit(1); } close (fd); }}




我建议您在担心优惠之前学会使用换行符



-

当我不得不依靠不足时,我更喜欢它是我自己的。

- 理查德希思菲尔德



I recommend that you learn to use line breaks before worrying
about optimization.
--
"When I have to rely on inadequacy, I prefer it to be my own."
--Richard Heathfield


On Thu,2003年11月13日15:02:50 -0800,Ben Pfaff写道:
On Thu, 13 Nov 2003 15:02:50 -0800, Ben Pfaff wrote:
我建议您在担心之前学会使用换行符
关于优化。
I recommend that you learn to use line breaks before worrying
about optimization.




哦,我的。它在Pan中看起来格式正确,我曾经发布它。

我不太确定什么是错的,但它绝对不应该全部

在一条线上!


-
http ://www.metawire.org - 开放主机


立即获取免费外壳和

加入不断增长的用户社区! />



Oh my. It looks properly formatted in Pan which I used to post it.
I''m not quite sure what''s wrong but it definitely shouldn''t have been all
on one line!

--
http://www.metawire.org - Open Hosting

Get your free shell today and
join a growing community of users!




这是尝试使用GNU重新发布。如果这不起作用,我将会知道会发生什么。


void loadContacts(){


int fd,bytesread;


if((fd = open(" contacts.data",O_RDONLY,0))!= -1){


struct contact * prev,* curr =(struct contact *)malloc(sizeof(struct contact));


if(curr){

if((bytesread = read(fd,curr,sizeof(struct contact)))== sizeof(struct contact)){

first.next = curr; //首先是一个全局结构,它是链表中的第一个条目

}否则{

free(curr); //有没有办法取消这个?

}

做{

prev = curr;

curr =(struct contact *)malloc(sizeof(struct contact));

prev-> next = curr;

} while((bytesread = read(fd) ,curr,sizeof(struct contact)))== sizeof(struct contact));

if(bytesread< sizeof(struct contact)){

prev-> ; next = NULL;


free(curr); //同样,这个?

}


}其他{

perror(" malloc.\ n);;

退出(1);


}


关闭(fd);

}

}


Here''s an attempt to repost using GNUs. If this doesn''t work, I
don''t know what will.

void loadContacts () {

int fd, bytesread;

if ( (fd = open("contacts.data", O_RDONLY, 0) ) != -1 ) {

struct contact *prev, *curr = (struct contact *)malloc(sizeof(struct contact));

if ( curr ) {
if ( ( bytesread = read(fd, curr, sizeof(struct contact))) == sizeof(struct contact) ) {
first.next = curr; // First is a global struct which is the first entry in the linked list
} else {
free (curr); // Is there a way to do away with this?
}
do {
prev = curr;
curr = (struct contact *)malloc(sizeof(struct contact));
prev->next = curr;
} while ( ( bytesread = read(fd, curr, sizeof(struct contact))) == sizeof(struct contact) );
if ( bytesread < sizeof(struct contact) ) {
prev->next = NULL;

free(curr); // Likewise, this?
}

} else {
perror("malloc.\n");
exit(1);

}

close (fd);
}
}


这篇关于想要优化这个程序,有什么建议吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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