免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1706 | 回复: 0
打印 上一主题 下一主题

env_init [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-05-11 17:11 |只看该作者 |倒序浏览

                                                                今天开始lab3代码完整工作,第一个函数env_init,e文注释如下
Mark all environments in 'envs' as free, set their env_ids to 0,  and insert them into the env_free_list.  
Insert in reverse order, so that the first call to env_alloc()
returns envs[0].
我翻译: 设置所有envs里的 environments为free, 设置它们的env_id 为0, 将其插入到链表env_free_list,以相反顺序插入,以便第一个node调用env_alloc(),返回 envs[0]。
可是代码写成如下
void
env_init(void)
{
    // LAB 3: Your code here.
    int i = 0;
   
    envs[0].env_id = 0;
     LIST_INIT(&env_free_list);
    for(i =1; i env_id = 0;
        env_alloc(&envs, envs.env_id);
        LIST_INSERT_HEAD(&env_free_list, envs, env_link);
        
     }
     return envs[0];
}
看了看weijiang同学http://blog.chinaunix.net/u1/37850/showart.php?id=299616的代码
发现两个问题
void
env_init(void)
{
    // LAB 3: Your code here.
    int i;
    for (i = NENV - 1; i >= 0; i--){
        envs.env_status = ENV_FREE;
        LIST_INSERT_HEAD(&env_free_list, &envs, env_link);
    }
}
有什么问题,
1. 顺序错误 :
   要求是逆向,写成正向;原因没有仔细看注释
2. 没有看到有这一项env_status,
3. void型函数,为什么又return。
今后注意:
1. 首先把注释翻译,一定要写道代码里;
2. 先把head file 读三遍;
3. env_alloc不是在这里调用的,语义要理解。
               
               
               
               
               
               
               
               
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/96302/showart_1924644.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP