代码审查? C中的内存管理 [英] Code Review? memory management in C

查看:49
本文介绍了代码审查? C中的内存管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件用不同的方式来写数字


---- 8< (剪切)--------

0:零,zilch ,, nada,,,,,空,无效,哦

1:one

7:七

2:两个也是

----> 8 ------------- -


我想读取该文件并将其放入动态内存中,例如


char ***同义词;

/ *假设一切都已经是malloc''d * /

同义词[0] [0] ="零" ;;

同义词[0] [1 ] =" zilch";

/ * ... * /

同义词[4] [0] = NULL;

/ * ... * /

同义词[7] [0] =" seven" ;;

同义词[7] [1] = NULL;

同义词[8] = NULL;


经过几天(大部分是晚上)挣扎之后,

程序中的那部分就是这样做的已完成。


我欢迎即将到来的周末让它休息一下,然后再为该程序编写

真正的理由:)

如果我发布200行新手代码,分布在三个文件,

for a在alt.comp.lang.learn.c-c ++或comp.lang.c上进行代码审查?


-

如果您要发布通过谷歌阅读< http://cfaj.freeshell.org/google>

I have a file with different ways to write numbers

---- 8< (cut) --------
0: zero, zilch,, nada, ,,,, empty , void, oh
1: one
7: seven
2: two, too
---- >8 --------------

I wanted to read that file and put it into dynamic memory, like

char *** synonym;
/* assume everything is already malloc''d */
synonym[0][0] = "zero";
synonym[0][1] = "zilch";
/* ... */
synonym[4][0] = NULL;
/* ... */
synonym[7][0] = "seven";
synonym[7][1] = NULL;
synonym[8] = NULL;

After a few days (mostly nights) of struggling with it, the part of the
program that does that is done.

I welcome the approaching weekend to give it a rest before coding the
real reason for the program :)
Is it ok if I post 200 lines of newbie code, spread over three files,
for a code review on either alt.comp.lang.learn.c-c++ or comp.lang.c?

--
If you''re posting through Google read <http://cfaj.freeshell.org/google>

推荐答案



" Pedro Graca" ; <他**** @ dodgeit.com>在消息中写道

news:sl ******************* @ ID-203069.user.individual.net ...

"Pedro Graca" <he****@dodgeit.com> wrote in message
news:sl*******************@ID-203069.user.individual.net...
我有一个文件用不同的方式来写数字

---- 8< (切)--------
0:零,zilch ,, nada,,,,,空,虚空,哦
1:一个
7:七> 2:两个,
----> 8 --------------

我想读取该文件并将其置于动态记忆,比如

char ***同义词;
/ *假设一切都已经是malloc''d * /
同义词[0] [0] ="零" ;;
同义词[0] [1] =" zilch";
/ * ... * /
同义词[4] [0] = NULL;
/ * .. 。* /
同义词[7] [0] =" seven" ;;
同义词[7] [1] = NULL;
同义词[8] = NULL;
<经过几天(大部分是夜晚)的挣扎之后,
程序的部分就完成了。

我欢迎即将到来的周末让它休息一下编码程序的真正原因:)

如果我发布200行新手代码,分布在三个文件,
以便在任一alt上进行代码审查,这样可以。 comp.lang.learn.c-c ++或comp.lang.c?
I have a file with different ways to write numbers

---- 8< (cut) --------
0: zero, zilch,, nada, ,,,, empty , void, oh
1: one
7: seven
2: two, too
---- >8 --------------

I wanted to read that file and put it into dynamic memory, like

char *** synonym;
/* assume everything is already malloc''d */
synonym[0][0] = "zero";
synonym[0][1] = "zilch";
/* ... */
synonym[4][0] = NULL;
/* ... */
synonym[7][0] = "seven";
synonym[7][1] = NULL;
synonym[8] = NULL;

After a few days (mostly nights) of struggling with it, the part of the
program that does that is done.

I welcome the approaching weekend to give it a rest before coding the
real reason for the program :)
Is it ok if I post 200 lines of newbie code, spread over three files,
for a code review on either alt.comp.lang.learn.c-c++ or comp.lang.c?




IMO即是一个新闻组的帖子很多。我建议你把它放在一个网站上,然后把这个网址贴在这里用

你的意见/问题。然后那些不感兴趣的人不需​​要花时间下载,而那些是,b $ b可以。


-Mike



IMO that is a but much for a newsgroup post. I suggest
you put it on a web site, then post the URL here with
your comments/questions. Then those who are not interested
don''t need to spend the time downloading, and those who are,
can.

-Mike


Mike Wahler写道:
Mike Wahler wrote:
" Pedro Graca" <他**** @ dodgeit.com>在消息中写道
新闻:sl ******************* @ ID-203069.user.individual.net ...
"Pedro Graca" <he****@dodgeit.com> wrote in message
news:sl*******************@ID-203069.user.individual.net...
我有一个文件用不同的方式来写数字
< snip>我想读取该文件并将其放入动态内存
< snip>如果我在alt.comp.lang.learn.c-c ++或comp.lang.c上发布200行新手代码,分布在三个文件上,
进行代码审查,是否可以?
I have a file with different ways to write numbers <snip> I wanted to read that file and put it into dynamic memory <snip> Is it ok if I post 200 lines of newbie code, spread over three files,
for a code review on either alt.comp.lang.learn.c-c++ or comp.lang.c?



IMO对新闻组的帖子非常重要。我建议你把它放在一个网站上,然后在这里张贴你的评论/问题。然后那些不感兴趣的人不需​​要花时间下载,而那些人,
可以。



IMO that is a but much for a newsgroup post. I suggest
you put it on a web site, then post the URL here with
your comments/questions. Then those who are not interested
don''t need to spend the time downloading, and those who are,
can.




好​​的。我把代码发布到了

< http://hexkid.blogspot.com/2006/02/memory-management-in-c.html>


我真正的问题是,如果程序没有内存泄漏或一些其他不好的事情与分配调用。


除此之外,任何其他提示/关于如何做得更好的提示将非常感谢。

PS。我不得不稍微调整一下代码(''<'和''&''),让它显示正确的显示。希望它不会成为人们无法编辑的混乱

将其复制/粘贴到他们的计算机上。


-

如果您通过Google发布,请阅读< http://cfaj.freeshell.org/google>



Ok. I posted the code to
<http://hexkid.blogspot.com/2006/02/memory-management-in-c.html>

My real question is if the program does not have memory leaks or some
other bad things with the allocation calls.

Other than that, any other hints/tips on how to do things better would
be very much appreciated.
PS. I had to tweak the code a bit (the ''<''s and the ''&''s) to have it
display properly. Hope it doesn''t become an uncompilable mess for people
who copy/paste it to their computers.

--
If you''re posting through Google read <http://cfaj.freeshell.org/google>


F''up2 clc


Pedro Graca写道:
F''up2 clc

Pedro Graca wrote:
好的。我把代码发布到
< http://hexkid.blogspot.com/2006/02/memory-management-in-c.html>

我真正的问题是如果程序分配电话没有内存泄漏或其他坏事。

除此之外,关于如何做得更好的任何其他提示/提示将非常多赞赏。


我会评论你的main()函数,所以我在这里引用那部分:/ * main.c
*
* Pedro Graca,2006-02 -09
*此代码属于公共领域
* * /

#include< stdio.h>
#include< stdlib.h>
#include" selfref.h"
int main(int argc,char * argv [])


char ** argv
更清楚。

{
char *** synonyms = NULL;
FILE * fp;
char line [MAX_LINE_LENGTH + 1];

if(argc< 2){
fprintf(stderr," Usage:%s FILENAME\ n",argv [0]);


注意:argv [0]可以为NULL。这个

argv [0]? argv [0]:"< program>"

或类似。退出(EXIT_FAILURE);
}


在这里,您已经完成了输入的验证。现在,真正的

事情开始了。将它放入一个函数(使用const char *

参数)并调用此函数。

fp = fopen(argv [1]," r");
if(!fp){
fprintf(stderr,无法打开文件。\ n);
退出(EXIT_FAILURE);
}
while(fgets (line,MAX_LINE_LENGTH,fp)){
if(parse_line(line,& synonyms))
fprintf(stderr,Oops \ n);
}
FCLOSE(FP);


注意:如果文件完全为空,同义词可能仍然在这里为
为空。

parse_line中的错误相同()。


显然(通过查看下面的内容),parse_line()会做两件事:

- 如果= = NULL:分配同义词(和也许设置

同义词[0] = NULL);设置一些内部行号反对0.

- 总是:用指向

解析的东西的指针替换同义词[行号],增加行号,设置同义词[行号] = NULL


这太过分了。

将其分解为

1)初始化,

2)解析,并且

3)在同义词数组中添加另一个项目。

{/ *验证程序* /
int nidx = 0,syn_idx; < while(synonyms [nidx]){


如果同义词为NULL,这将非常错误。

syn_idx = 0;
printf("%d:",nidx);
while(synonyms [nidx] [syn_idx]){
printf("%s",synonyms [nidx] [syn_idx ]);
++ syn_idx;
if(synonyms [nidx] [syn_idx])
printf(",");
}
++ nidx ;
printf(" \ n");
}
}


使其成为自己的功能。


{/ * fre e memory * /
int nidx = 0,syn_idx;

while(synonyms [nidx]){
syn_idx = 0;
while(synonyms [nidx] [ syn_idx]){
free(同义词[nidx] [syn_idx]);
++ syn_idx;
}
free(同义词[nidx] [syn_idx]);
free(同义词[nidx]);
++ nidx;
}
free(同义词[nidx]);


你忘了免费(同义词);

}


经验法则:分配的角色东西,释放了

的东西。提供专用的分配处理和随附的释放功能



#ifdef _WIN32
printf("按< ENTER> \ n");
getchar();
#endif


足够公平。

返回0;
}


总而言之,相当不错。

在设计中工作并将其分解为易于测试

函数。第2步:测试这些功能。

PS。我不得不稍微调整代码(''<'和''&'')以使其正确显示。希望它不会成为人们将其复制/粘贴到他们的计算机上的无法编辑的混乱。
Ok. I posted the code to
<http://hexkid.blogspot.com/2006/02/memory-management-in-c.html>

My real question is if the program does not have memory leaks or some
other bad things with the allocation calls.

Other than that, any other hints/tips on how to do things better would
be very much appreciated.
I will comment on your main() function, so I quote that part here: /* main.c
*
* Pedro Graca, 2006-02-09
* This code is in the public domain
* */

#include <stdio.h>
#include <stdlib.h>
#include "selfref.h"

int main(int argc, char * argv[])
char ** argv
is clearer.
{
char *** synonyms = NULL;
FILE * fp;
char line[MAX_LINE_LENGTH + 1];

if (argc < 2) {
fprintf(stderr, "Usage: %s FILENAME\n", argv[0]);
Note: argv[0] can be NULL. Make this
argv[0] ? argv[0] : "<program>"
or similar. exit(EXIT_FAILURE);
}
Here, you have finished validating the input. Now, the real
thing starts. Put it into a function (with const char *
parameter) and call this function.
fp = fopen(argv[1], "r");
if (!fp) {
fprintf(stderr, "Unable to open file.\n");
exit(EXIT_FAILURE);
}
while (fgets(line, MAX_LINE_LENGTH, fp)) {
if (parse_line(line, &synonyms))
fprintf(stderr, "Oops\n");
}
fclose(fp);
Note: If the file is completely empty, synonyms may still
be NULL here.
The same for errors in parse_line().

Obviously (by looking below, too), parse_line() does 2 things:
- If synonyms==NULL: Allocate synonyms (and maybe set
synonyms[0]=NULL); set some internal "line number" counter to 0.
- Always: replace synonyms[line number] by a pointer to the
parsed stuff, increase line number, set synonyms[line number]=NULL

This is one thing too much.
Break this down into
1) initialisation,
2) parsing, and
3) adding another item to the synonyms array.
{ /* validate program */
int nidx=0, syn_idx;

while (synonyms[nidx]) {
If synonyms is NULL, this will go terribly wrong.
syn_idx = 0;
printf("%d: ", nidx);
while (synonyms[nidx][syn_idx]) {
printf("%s", synonyms[nidx][syn_idx]);
++syn_idx;
if (synonyms[nidx][syn_idx])
printf(", ");
}
++nidx;
printf("\n");
}
}
Make this a function of its own.

{ /* free memory */
int nidx=0, syn_idx;

while (synonyms[nidx]) {
syn_idx = 0;
while (synonyms[nidx][syn_idx]) {
free(synonyms[nidx][syn_idx]);
++syn_idx;
}
free(synonyms[nidx][syn_idx]);
free(synonyms[nidx]);
++nidx;
}
free(synonyms[nidx]);
You forget to free(synonyms);
}
Rule of thumb: The role that allocates the stuff, frees the
stuff. Provide a dedicated allocation handling and an
accompanying deallocation function.
#ifdef _WIN32
printf("Press <ENTER>\n");
getchar();
#endif
Fair enough.
return 0;
}
All in all, rather nice.
Work at the design and break things down into easily testable
functions. Step 2: Have a test for these functions.
PS. I had to tweak the code a bit (the ''<''s and the ''&''s) to have it
display properly. Hope it doesn''t become an uncompilable mess for people
who copy/paste it to their computers.




提供很好看的以及下载版本。


注意:考虑使用ggets()/ fggets()来读取行。

这些是非标准的但是那里至少是一个公共领域

版本(由Chuck Falconer提供)。

优点:没有行长度限制,自动分配

内存为该行(这意味着没有不必要的复制

你的情况)。


我只看了很短的实际的selfref.c和

parse_line()。不要试图通过将if和

后面的语句if(condition)放在一行来保存行。

这很容易模糊。特别是如果一个人从浏览器中复制了你的来源,那么就会丢失

缩进。

同样适用于循环等。

干杯
Michael

-

电子邮件:我的是/ at / gmx / dot / de地址。



Provide a "nice to look at" as well as a download version.

Note: Consider using ggets()/fggets() for reading the lines.
These are non-standard but there is at least one public domain
version (by Chuck Falconer).
Advantage: No line length restriction, automatic allocation of
the memory for the line (which means no unnecessary copying in
your case).

I have looked only shortly at the actual selfref.c and
parse_line(). Do not try to save lines by putting if and
the statement following if (condition) in one line.
This easily can obscure things. Especially if one loses
indentation copying over your source from the browser.
The same holds for loops etc, too.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


这篇关于代码审查? C中的内存管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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