求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);
}
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);
}