avatar
求paper review机会~~# Biology - 生物学
a*u
1
I think my answers to Question 7 and 8 are correct, but they said they are
not correct, why?
I already tested the code by Visual C++ (c compiler).
Thank you very much.
Line in file Contents
30 int * someIDs, theFirst, *r;
110 someIDs =GetSomeIDs(); /* defined below */
111 theFirst = someIDs [0];
112 r= ReorderIDs(someIDs);
113-150 /* we want to use ‘theFirst’ and ‘r’ here*/

499 /*-------- GetSomeIDs-----*/
500 int * GetSomeIDs()
501 {
502 int ids[8];
503-550 /* The ids are defined here */
551 return ids;
552 }
1. Is GetSomeIDs() a reasonable function?
No, runtime error may happen.
2. Is there a different way to write line 500 which preserves the same
effective prototype? If so, what is it?
Yes, int * GetSomeIDs(void)
3. What will ‘theFirst’ contain after line 111 is executed? Is this
deterministic? Why?
“theFirst” should contain the integer number stored in the “someIDs[0]”.
The answer, once again, lies in the issue of where the memory is allocated,
and how much activity there is on the system stack, but the general answer
is 'no' - while the odds are it would still be the same value it was set to
in GetSomeIDs() (because there haven't been any function calls to trash the
old stack), it would depend on the implementation (and possibly on things
like interrupts and virtual memory layout, as well).
4. What line(s) not given should be provided for compilation?
Line “int * GetSomeIDs();” should be provided before line 30 for
compilation.
5. Is GetSomeIDs() good practice, and why?
No. The variable ids is allocated on stack, but returned to the result in
the form of heap. It is difficult to re-use the function.
6. Might the code shown execute at all?
If the code is added “include” related libraries, “main” structure and
declaration, it will execute. But, runtime errors may occur.
Generally, I never write the function like this one. Why do we want to
implement a function? According to software engineering, it should be easy
to read and to re-use. Everybody should predict the results of the function.
But the function “GetSomeIDs” is too wield.
7. Correct the problems with GetSomeIDs(), and add some additional
functionality to it, as follows. A single new version of the function
should be provided.
The new version should:
a) Maintain the same "int *" return type which returns a pointer to fixed
sized array of ints.
b) IN ADDITION to its regular function return, provide to its calling
functions a usable array of pointers to aliasID structures. The length of
this array of pointers is returned by a call to GetNumberOfAliases(), which
you may call only from within GetSomeIDs().
c) Use the structure and the functions defined below:
typedef struct {
char* alias; /* '\0'-terminated C string */
int specific_id;
} aliasID;
/* How many structures should be pointed to by the array
*/
int GetNumberOfAliases(void);
/* Get a pointer to the next structure. The structure itself
* will be filled with data.
* Caller is responsible for the cleanup of the returned structure
* and its content. The latter are allocated in
* dynamic memory.
*/
aliasID * GetNextAlias(void);
Do NOT(!) use "C++" syntax or language contructs.
This should be written in plain “C”.
Use good programming practice, as much as these instructions allow.
int * GetSomeIDs(aliasID** aID,int * sz){
int i = 0;
int * ids = 0;
ids = (int *) malloc(sizeof(int)*8);
*sz=GetNumberOfAliases();
/* The ids are defined here */
for(i=0;iaID[i]=GetNextAlias();
}
return(ids);
}
8 . Write a function which calls GetSomeIDs() as described in (7) above,
prints out all data returned by it, and cleans up resources allocated in
dynamic memory.

For both (7), and (8), above, DO NOT use "C++" syntax or language
contructs. These should be written in plain “C”.
Use good programming practice, as much as these instructions allow.
void ProcessIDs(){
aliasID ** aID=0;
int numaliasID=0;
int i=0;
int * size = 0;
int* ID=0;
size= (int *)malloc(sizeof(int));
numaliasID=GetNumberOfAliases();
aID=(aliasID**)malloc(sizeof(aliasID*)*numaliasID);
ID=GetSomeIDs(aID,size);
/* print out ids */
for(i=0;i<8;i++)
printf("%d\n",ID[i]);
/* print out alias nodes */
for(i=0;iprintf("aID[%d]: string=%s specific_id=%d\n",i,aID[i]->alias,aID
[i]->specific_id);
}
// release memory
free(size);
free(ID);
for(i=0;ifree(aID[i]->alias);
free(aID[i]);
}
free(aID);
}
avatar
s*8
2
如果下个月自己交了485, 只要我的485 is pending, 就算排期倒回去了, LD 还是可
以再交485吗?还是她必须要等到我的PD 再次CURRENT的时候才能再交485.
老婆现在不在美国,不知道是不是应该一起交表。 谢谢了
avatar
g*e
3
我家的即使湿度降到设定值风扇也不停,所以有点吵。
avatar
I*W
4
【 以下文字转载自 Joke 讨论区 】
发信人: csboy2007 (我叫包守腿。。来自火星的宇宙无敌包子控), 信区: Joke
标 题: :)第三辑动物搞笑有爱图(猫咪狗儿神马的,人类是浮云) 鸭梨籹
发信站: BBS 未名空间站 (Wed Mar 2 09:32:23 2011, 美东)
好久没有看到动物了吧,
好像有些是老的,不过看看玩玩乐乐吧:)
avatar
a*p
5
本人计算生物学专业博士毕业一年,在基因调控网络建模,系统生物学,细胞周期,肿
瘤生物学,单细胞和单分子成像分析方面有研究经验。跪求相关领域的paper review机
会~~
avatar
S*I
6
Should pass an array argument to the function instead of using malloc.

【在 a******u 的大作中提到】
: I think my answers to Question 7 and 8 are correct, but they said they are
: not correct, why?
: I already tested the code by Visual C++ (c compiler).
: Thank you very much.
: Line in file Contents
: 30 int * someIDs, theFirst, *r;
: 110 someIDs =GetSomeIDs(); /* defined below */
: 111 theFirst = someIDs [0];
: 112 r= ReorderIDs(someIDs);
: 113-150 /* we want to use ‘theFirst’ and ‘r’ here*/

avatar
s*n
7
必须等到排期到
avatar
J*G
8
yes

【在 g***e 的大作中提到】
: 我家的即使湿度降到设定值风扇也不停,所以有点吵。
avatar
f*n
9
逗!
avatar
a*u
10
Q7:the difference between heap and stack
avatar
g*e
11
不知道如果挪到furnace room(有个百叶门隔开)的话除湿效果差多少。

【在 J**G 的大作中提到】
: yes
avatar
t*i
12
我也要去給我家狗狗們弄SUPER MAN的衣服! 太討喜了!
avatar
r*t
13
这个问题好像有点多,除了到处分配内存,数字 8 出现不同地方以外,
*size free 了以后又 deference 了,这个好像不可以吧(不过我没上机试过)

【在 a******u 的大作中提到】
: I think my answers to Question 7 and 8 are correct, but they said they are
: not correct, why?
: I already tested the code by Visual C++ (c compiler).
: Thank you very much.
: Line in file Contents
: 30 int * someIDs, theFirst, *r;
: 110 someIDs =GetSomeIDs(); /* defined below */
: 111 theFirst = someIDs [0];
: 112 r= ReorderIDs(someIDs);
: 113-150 /* we want to use ‘theFirst’ and ‘r’ here*/

avatar
q*x
14
题太长,真看不下去。

【在 a******u 的大作中提到】
: I think my answers to Question 7 and 8 are correct, but they said they are
: not correct, why?
: I already tested the code by Visual C++ (c compiler).
: Thank you very much.
: Line in file Contents
: 30 int * someIDs, theFirst, *r;
: 110 someIDs =GetSomeIDs(); /* defined below */
: 111 theFirst = someIDs [0];
: 112 r= ReorderIDs(someIDs);
: 113-150 /* we want to use ‘theFirst’ and ‘r’ here*/

avatar
j*n
15
#2,3,4, 7,8 都错了,LZ还是对C了解不深,这些题目看起来容易,实际挺难的,要
用真正的C
avatar
j*n
16
提示一下:
#2 int (*GetSomeIDs())[8]
avatar
j*n
17
#5 结论是对的,但是解释错了
avatar
j*n
18
#7 提示一下:
int (* GetSomeIDs(aliasID** aID,int * sz))[8]
{
.....
}
avatar
M*A
19
标准c
int main(void) {
}
如果c++也是这样写,会被说成“die-hard c programmer”
avatar
f*n
20
敢问是什么公司?
int * GetSomeIDs(aliasID** aID,int * sz)
这个定义是题目给的还是你自己想的阿?
avatar
h*k
21
你的答案确实有问题,第七问,caller都不知道aliasId数组的大小,它怎么能预先分
配空间?所以题目肯定是要求你在函数中动态分配
avatar
h*k
22
题目要求The length of
this array of pointers is returned by a call to GetNumberOfAliases(), which
you may call only from within GetSomeIDs(). 你可能没注意到。
avatar
a*6
23
C 是个啥?
avatar
s*g
24
499 /*-------- GetSomeIDs-----*/
500 int * GetSomeIDs()
501 {
502 int ids[8];
503-550 /* The ids are defined here */
551 return ids;
552 }
不能这么写吧,ids是局部变量,function结束后指针为junk,用heap内存存储才好
avatar
t*l
25
It has nothing to do with re use of a function.
You just can't return a pointer pointing to a local variable which will no
longer be accessible once you exit the function
Allocating memory using heap inside that function is not a good practice
either because users can easily forget to free the dynamic mem
A better way is to let the caller define and release a dynamic mem and the
function only does the calculation.
[在 arayzhou (aray) 的大作中提到:]

:I think my answers to Question 7 and 8 are correct, but they said they are
:...........
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。