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;

??}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值