国外一家公司的考试题(ZT)

博客围绕C语言代码展开,探讨了变量赋值、函数合理性、编译所需声明等问题,如getbuf()函数返回局部变量地址不安全。还给出SQL查询语句统计基因数量,同时阐述了系统调用和库函数的区别,系统调用涉及内核模式和上下文切换,库函数多在用户空间执行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Line Contains

50 char *b, q, *r;
200 b = getbuf ();
201 q = *b;
212 R = anotherfunction (b);
213-2003 /* 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?

2.      Is there an alternative, but equivalent, way to write line 2000? If so, what is it?

3.      Is getbuf() a reasonable function?

4.      Will getbuf() execute at all?

5.      Please comment on line 2051.

6.      Is getbuf() good practice, and why?

7.      What line not given should be provided for compilation?

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 no use) C++  
syntax. Include statement in called and calling functions. Use food programming practice.

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 would you find how many genes are on each and every clone? Please provide the SQL.

10.  What's the difference between system calls and library functions?

====================================================================

1.      What will be in variable 'q' after line 201 is executed? Under what conditions might this not be so?
   After line 201 is executed, in variable ‘q’ would be buff[0]. But function getbuf() return address of a local variable, so it is not safe. If some code resulted in stack destroyed(overwriten), q and b are undefined.



2.      Is there an alternative, but equivalent, way to write line 2000? If so, what is it?
char [] getbuf ()


3.      Is getbuf() a reasonable function?
No. function getbuf() return address of a local variable, so it is not safe.



5.      Please comment on line 2051.
           /*rerurn address of a local variable, must carefully*/



7.      What line not given should be provided for compilation?
1) anotherfunction() must have a declararion.
2) Variable R must have a declaration.



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 would you find how many genes are on each and every clone? Please provide the SQL.
Select cid, count(gid) from table_name group by cid;


10, What's the difference between system calls and library functions?

From a programming perspective, there is no difference between a system call or any other function call.

In general, a "system call" is the API through which a program can request OS level services such as read/write/open/close devices (disk, comm, etc.) utilizing the "kernel mode" processing. This is typically the "way in" to the device driver itself from an application. The basic difference is that when a system call is used, this will generate a "context switch" (interrupt) which can be expensive in terms of execution time (in RT systems).

A "library function" can be built upon and around system calls to allow for "the perception" of varied access to devices. It can also use no system level service at all. In general, library functions execute predominantly in "user space" unless they invoke a system level service request (system call).



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值