- 论坛徽章:
- 0
|
今天开始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 |
|