有一个 interview 的 C++ 评估测试,感觉做得还行,可对方说太 junior,可请给点意见。答案错在哪里?谁能指点指点
LINE Contains
50 char * b, q, *r;
200 b=getbuf();
201 q = *b;
212 r= anotherfunction(b);
213-300 /* we want to use ‘q’ and ‘r’ here*/
2000 char * getbuf()
2001 {
2002 char buff[8];
2003-2050 /* unspecified, buff defined here *./
2051 return (char *) buff;
2052 }
1. What will be in variable ‘q’ after line 201 is executed? Under what conditions might this not be so?
Q would be the first character of a character array returned by getbuf(). If getbuf returns a null string, that would be not so.
2. Is there an alternative, but equivalent, way to write line 2000? If so, what is it?
There are a number of other ways to define char* getbuf(), but either way would require some modifications of other code lines. For example, there are two other ways as follows:
(1) The easiest way would be returning a real c++ string, not a char string in c-style:
String getbuf()
In this case, we also need to change the type of b from char* to string.
(2) The other way is to define char buff[8] out of function getbuff, and then pass buff as a function argument into the getbuff function by reference. Something like:
int getbuff( char & buff)
int is -1 or 0;
Either way we need to make some corresponding changes to other parts.
3. Is getbuf() a reasonable function?
Getbuf() returns a c-style string, I think this is a reasonable function in terms of what it tries to accomplish. In fact, in standard C string library, there are quite a number of functions which return strings of type char*, like strncpy, strncat, etc. But as I am going to discuss below, the way of returning the char string by pointers used in this particular function is not good.
4. Will getbuf() execute at all?
It should not compile and execute, because the pointer returned by the getbuff() function points to a local character array whose lifetime ended when that function finished executing and returned control to main. In order to make it work, we have a number of ways to slightly modify it, as I will discuss in Question 6, 7 and 8.
5. Please comment on line 2051.
We don’t need to return (char*)buff, because we have already defined buff as a char array pointer before. Just returning the pointer buff is enough. Although through either way we can get the right result, the latter one can avoid redundancy and therefore is strongly preferred.
6. Is getbuf() good practice, and why?
From the answer to Question 4, we know that this code should not be able to execute. So, it is not a good practice. The correcting remedies would be either adopting the C++ String, or modifying the way this function is realized. I will talk about them in 7 and 8.
7. What line not given should be provided for compilation?
Among others, I can provide three reasonable ways to make it compliable:
Since the problem is that a local variable will die out of its scope, therefore one solution is to extend the lifetime of that character array to the life of the program execution itself. We can define the character array a global variable, not in the scope of the function getbuff().
The other solution is to define the character array as a static local variable. As a static variable, its lifetime still persists after getbuff() finishs executing and returns control to the calling function, as follows:
static char buff[8];
The third way is to use dynamically allocated memory to tackle this problem, as follows:
char *b, q, *r;
b = getbuff();
q = *b;
/*
Other statements……
*/
delete [] b;
;;;;;;;;;
char* getbuff ()
{
const in size = 8;
char* buff;
buff = new char[size];
return buff;
}
The second and the third solution are better than the first one.
8. How, exactly, could one get a second ‘char *’ to use back from this function? Be specific in terms of the exact syntax needed. (That is, provide code.) Another way to state this question is how can this function be modified to return a ‘char *’ (that is, it maintains the same return type) from the function, and an additional ‘char *’ value in one function call. Please make sure that your answer will work even if the size of the char * desired is not known in the outside calling function. Avoid (do not use) C++ syntax. Include statements in called and calling functions. Use good programming practice.
I am not quite clear about this question, but I hope I have answered this question through questions 1-7.
9. For those candidates who know SQL: There is a table with gene_ids (‘gid’) and clone_ids (‘cid’). Each gene only resides on a single clone and each clone may contain multiple genes. How do you find how many genes are on each and every clone? Please provide the SQL.
SELECT cid, COUNT(cid)
From Table_Name
GROUP BY gid
10. What's the difference between system calls and library functions?
System call is a subroutine provided by the system and executed in the kernel, and therefore it is a part of the operating system; Library function is a normal function, which include C standard library.
C++ 高手请进
所有跟帖:
•
你没有多少PROGRAMMING的经验吧?
-不是高手...-
♀
(400 bytes)
()
09/08/2005 postreply
21:08:30
•
感觉好像回答的还可以啊
-不是太明白-
♀
(0 bytes)
()
09/08/2005 postreply
23:49:17
•
question 9
-zy001-
♂
(107 bytes)
()
09/09/2005 postreply
04:08:02
•
回复:C++ 高手请进
-Sic-
♀
(1436 bytes)
()
09/09/2005 postreply
10:31:22
•
这位是高手, 不得不服
-不是高手...-
♀
(190 bytes)
()
09/09/2005 postreply
13:42:50
•
回复:C++ 高手请进... very interestin
-nima_hehe-
♀
(2373 bytes)
()
09/09/2005 postreply
16:15:37
•
funny
-questions!-
♀
(0 bytes)
()
09/09/2005 postreply
23:47:36
•
回复:C++ 高手请进
-你该这样答-
♀
(1124 bytes)
()
09/10/2005 postreply
21:55:04