linux软件工程师笔试题,C/C++软件工程师笔试题
1,程序設計(可以用自然語言來描述,不編程):C/C++源代碼中,檢查花括弧(是“(”與
“)”,“{”與“}”)是否匹配,若不匹配,則輸出不匹配花括弧所在的行與列。
2,巧排數字,將1,2,...,19,20這20個數字排成一排,使得相鄰的兩個數字之和為一個素數,且
首尾兩數字之和也為一個素數。編程打印出所有的排法。
3,打印一個N*N的方陣,N為每邊字符的個數(?? 3〈N〈20
),要求最外層為“X”,第二層為“Y”,從第三層起每層依次打印數字0,1,2,3,...
例子:當N?? =5,打印出下面的圖形:
X?? X?? X?? X?? X
X?? Y?? Y?? Y?? X
X?? Y?? 0?? Y
X
X?? Y?? Y?? Y?? X
X?? X?? X?? X?? X
其他C/C++軟件工程師筆試題
如何定位全局數組的寫越界
??一個被大量引用的全局數組int
a[100],被寫越界了,這樣的情況如何定位?
????
??最簡單的方法是,將數組a[100]改為a[101],然后對訪問a[100]的地方設置斷點進行調試。因為a[100]應該是沒有人訪問的,如果訪問就是越界訪問,直接可以定位到該位置。
????
??另外:將函數定義成static類型可以防止該文件意外的其他文件中的函數調用此函數。
++i與i++的區別到底怎樣?
??i++和++i的 最重要的區別大家都知道就是
+1和返回值的順序。但,兩這還有一個區別(在C++中)就是i++在實現的時候,產生了一個local object class
INT;
??
??//++i 的版本
??INT INT::operator++()
??{
??
*this=*this+1;
?? return *this;
??}
??
??//i++ 的版本
??const INT
INT::operator ++(int)
??{
?? INT oldvalue=*this;
??
*this=*this+1;
?? return
oldvalue
??}
??
??所以從效率上來說++i比i++來的更有效率。具體細節你可以看More Effective C++
的M6
內存泄漏
2006-2-18 星期六(Saturday) 晴
??struct
chunk_t
??{
?? u_char *ptr;
?? size_t len;
??};
??
??int
key_switch(const struct RSA_public_key *k, R_RSA_PUBLIC_KEY
*publickey)
??{
?? chunk_t exponent,modulus;
??
??
publickey->bits =(k->k)*BITS_PER_BYTE;
??
?? modulus =
mpz_to_n(&(k->n),k->k);
?? exponent =
mpz_to_n(&(k->e),k->k);
??
??
memcpy(publickey->modulus+128,modulus.ptr,modulus.len);
??
memcpy(publickey->exponent+128,exponent.ptr,exponent.len);
??
?? ……
……
?? return
0;
??}
??
??象上面這樣的函數,其中在調用mpz_to_n的時候進行了malloc內存分配,別以為chunk_t
exponent,modulus;是局部變量就沒問題,如果函數退出前不釋放mpz_to_n申請的空間,就會存在內存泄漏問題。
??
??應該在……
……處加上代碼:
??freeanychunk(modulus);
??freeanychunk(exponent);
??
??指針釋放的問題早就知道了,但是實際應用中還是會因為沒注意到而忘了。由于分配內存使用的是對malloc封裝的函數alloc_bytes(),所以使用相關的內存泄漏調試工具會定位到alloc_bytes()函數里,根本不能定位到具體泄漏的地點。
??
??所以說對malloc/free進行二次封裝有它的好處,同時也會帶來壞處。
在linux下防止某個程序被運行兩次的方法
??通過文件鎖來實現,在程序運行的一開始,檢查某文件是否存在,如果存在則說明改程序已經在運行了,如果不存在則利用open語句創建該文件,程序退出時關閉并刪除此文件。
??
??具體代碼:
??
??static char file_lock[sizeof(ctl_addr.sun_path)] =
/var/run/file.pid;
??static bool file_lock_created = FALSE;
??
??static
int
??create_lock(void)
??{
?? int fd = open(file_lock, O_WRONLY |
O_CREAT | O_EXCL | O_TRUNC,
?? S_IRUSR | S_IRGRP | S_IROTH);
??
?? if
(fd < 0)
?? {
?? if (errno == EEXIST)
?? {
?? fprintf(stderr,
\"file: lock file \"%s\" already existsn\", file_lock);
??
exit_file(10);
?? }
?? else
?? {
?? fprintf(stderr, \"file: unable
to create lock file \"%s\" (%d %s)n\"
?? , file_lock, errno,
strerror(errno));
?? exit_file(1);
?? }
?? }
?? file_lock_created =
TRUE;
?? return fd;
??}
??
??static bool
??fill_lock(int
lockfd)
??{
?? char buf[30]; /* holds \"n\" */
?? pid_t pid;
?? int
len;
??
?? pid = getpid();
?? len = snprintf(buf, sizeof(buf), \"%un\",
(unsigned int) pid);
?? bool ok = len > 0 && write(lockfd, buf,
len) == len;
??
?? close(lockfd);
?? return
ok;
??}
??
??static void
??delete_lock(void)
??{
?? if
(file_lock_created)
?? {
?? //delete_ctl_socket();
??
unlink(file_lock); /* is noting failure useful? */
??
}
??}
C/C++軟件工程師筆試題
??將任意證書N分解成多個互不相同的正整數的和,并打印所有可能的組合方式。例如N=6,組合方式有1+5,2+4,1+2+3。
??#include \"stdafx.h\"
??#include
\"stdlib.h\"
??
??static int n;
??int *a;
??static int total =0
;
??void output(int s){
?? int i=1;
?? printf(\"%d =
%d\",n,a[i]);
?? for(i=2; i<=s; i++){
?? printf(\"+%d\",a[i]);
??
}
?? printf(\"n\");
??}
??
??int filter(int s){
?? int i,j;
??
if(s==1)return -1;
?? for(i=s;i>0;i--)
?? for(j=1;j??
if(a[i]==a[j])
?? return -1;
?? }
?? return 0;
??}
??void dfs(int
d,int low,int rest){
?? //printf(\"d = %d ,low = %d ; rest
=%dn\",d,low,rest);
?? int i;
?? if(rest == 0){
??
if(filter(d-1)==0){
?? total ++;
?? output(d-1);
?? }
?? }
??
if(low>rest) {
?? //printf(\"1111111111111n\");
?? return ;
??
}
?? for(i=low;i<=rest;i++){
?? a[d]=i;
?? dfs(d+1,i,rest-i);
??
}
??}
??
??int main(int argc, char* argv[])
??{
??
?? int
num;
?? printf(\"input:\");
?? scanf(\"%d\",&num);
?? n=num
;
??
?? a =(int *)malloc(sizeof(int)*(n+1));
??
?? dfs(1,1,n);
??
printf(\"total = %dn\",total);
?? free(a);
?? return 0;
??}
總結
以上是生活随笔為你收集整理的linux软件工程师笔试题,C/C++软件工程师笔试题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 美团未来发展方向 想创业做外卖的一定
- 下一篇: 安邦盈泰5号是不是骗局