有些面试题啊,得猜出题者的心意

oN5GrzoN 2016-01-22 04:37:05
原来,我是不屑面试之前做些笔试题的,毕竟做了十几年了,感觉能应付吧.
面试了几家,发现还真不是.有些题,得猜出题者的心意,
比如,常见的自己做一个strcpy的函数(下面是我搜到的)

/*
编写strcpy函数(10分)
已知strcpy函数的原型是
char *strcpy(char *strDest, const char *strSrc);
其中strDest是目的字符串,strSrc是源字符串。
(1)不调用C++/C的字符串库函数,请编写函数 strcpy
(2)strcpy能把strSrc的内容复制到strDest,为什么还要char * 类型的返回值?
答:为了 实现链式表达式。 // 2分
例如 int length = strlen( strcpy( strDest, “hello world”) );
*/


#include <assert.h>
#include <stdio.h>
char*strcpy(char*strDest, const char*strSrc)
{
assert((strDest!=NULL) && (strSrc !=NULL)); // 2分
char* address = strDest;   // 2分
while( (*strDest++=*strSrc++) !='\0' )       // 2分
NULL;
return address ;    // 2分
}

如果这么做,就我上家面试的,就通不过.
我已经猜到他会考strDest,和strSrc地址相同的判断了,特意加了个
if(strSrc == strDest) return strSrc ;

后来他跟我聊,为什么没有判断strSrc 和 strDest会有重叠的情况.
比如 char* src = "abcd"; char* dest = scr+1;
strcpy(src , dest );
然后他问我这种情况要怎么办. 我老实说,真不清楚strcpy具体怎么处理这种情况,如果是日常工作,要写这样的函数,具体按照需求文档,可能需要抛异常,可能需要严格的判断,也可能不判断,因为判断也需要消耗时间,具体看函数需求.(而且,说实话,我现在常用C++,都是直接用面向对象的如string,CString之类的,不怎么经常用strcpy).

但是,面试者认为我的答案不合他的意……
...全文
185 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
oN5GrzoN 2016-01-26
  • 打赏
  • 举报
回复
引用 3 楼 fly_dragon_fly 的回复:
这是故意找茬吧, 要求就两点1)已经完成,2)鬼知道你要怎么用,至于重叠,地址可不可写,传入参数是否有效, 都属于扯蛋的范围, 我接口告诉你不能这样用, 你偏要用, 那应该自己负责
那个面试官,看年龄,感觉像刚工作2、3年的样子.问的问题特别浅,又很没意义.估计就是他现在工作中遇到了个什么,就问了.
赵4老师 2016-01-26
  • 打赏
  • 举报
回复
为什么不参考 C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\intel\strcat.asm
	page	,132
	title	strcat - concatenate (append) one string to another
;***
;strcat.asm - contains strcat()	and strcpy() routines
;
;	Copyright (c) Microsoft	Corporation. All rights	reserved.
;
;Purpose:
;	STRCAT concatenates (appends) a	copy of	the source string to the
;	end of the destination string, returning the destination string.
;
;*******************************************************************************

	.xlist
	include	cruntime.inc
	.list


page
;***
;char *strcat(dst, src)	- concatenate (append) one string to another
;
;Purpose:
;	Concatenates src onto the end of dest.	Assumes	enough
;	space in dest.
;
;	Algorithm:
;	char * strcat (char * dst, char	* src)
;	{
;	    char * cp =	dst;
;
;	    while( *cp )
;		    ++cp;	    /* Find end	of dst */
;	    while( *cp++ = *src++ )
;		    ;		    /* Copy src	to end of dst */
;	    return( dst	);
;	}
;
;Entry:
;	char *dst - string to which "src" is to be appended
;	const char *src	- string to be appended	to the end of "dst"
;
;Exit:
;	The address of "dst" in EAX
;
;Uses:
;	EAX, ECX
;
;Exceptions:
;
;*******************************************************************************

page
;***
;char *strcpy(dst, src)	- copy one string over another
;
;Purpose:
;	Copies the string src into the spot specified by
;	dest; assumes enough room.
;
;	Algorithm:
;	char * strcpy (char * dst, char	* src)
;	{
;	    char * cp =	dst;
;
;	    while( *cp++ = *src++ )
;		    ;		    /* Copy src	over dst */
;	    return( dst	);
;	}
;
;Entry:
;	char * dst - string over which "src" is to be copied
;	const char * src - string to be	copied over "dst"
;
;Exit:
;	The address of "dst" in EAX
;
;Uses:
;	EAX, ECX
;
;Exceptions:
;*******************************************************************************


	CODESEG

%	public	strcat,	strcpy	    ; make both	functions available
strcpy	proc \
	dst:ptr	byte, \
	src:ptr	byte

	OPTION PROLOGUE:NONE, EPILOGUE:NONE

	push	edi		    ; preserve edi
	mov	edi,[esp+8]	    ; edi points to dest string
	jmp	short copy_start

strcpy	endp

	align	16

strcat	proc \
	dst:ptr	byte, \
	src:ptr	byte

	OPTION PROLOGUE:NONE, EPILOGUE:NONE

	.FPO	( 0, 2,	0, 0, 0, 0 )

	mov	ecx,[esp+4]	    ; ecx -> dest string
	push	edi		    ; preserve edi
	test	ecx,3		    ; test if string is	aligned	on 32 bits
	je	short find_end_of_dest_string_loop

dest_misaligned:		    ; simple byte loop until string is aligned
	mov	al,byte	ptr [ecx]
	add	ecx,1
	test	al,al
	je	short start_byte_3
	test	ecx,3
	jne	short dest_misaligned

	align	4

find_end_of_dest_string_loop:
	mov	eax,dword ptr [ecx] ; read 4 bytes
	mov	edx,7efefeffh
	add	edx,eax
	xor	eax,-1
	xor	eax,edx
	add	ecx,4
	test	eax,81010100h
	je	short find_end_of_dest_string_loop
	; found	zero byte in the loop
	mov	eax,[ecx - 4]
	test	al,al		    ; is it byte 0
	je	short start_byte_0
	test	ah,ah		    ; is it byte 1
	je	short start_byte_1
	test	eax,00ff0000h	    ; is it byte 2
	je	short start_byte_2
	test	eax,0ff000000h	    ; is it byte 3
	je	short start_byte_3
	jmp	short find_end_of_dest_string_loop
				    ; taken if bits 24-30 are clear and	bit
				    ; 31 is set
start_byte_3:
	lea	edi,[ecx - 1]
	jmp	short copy_start
start_byte_2:
	lea	edi,[ecx - 2]
	jmp	short copy_start
start_byte_1:
	lea	edi,[ecx - 3]
	jmp	short copy_start
start_byte_0:
	lea	edi,[ecx - 4]
;	jmp	short copy_start

;	edi points to the end of dest string.
copy_start::
	mov	ecx,[esp+0ch]	    ; ecx -> sorc string
	test	ecx,3		    ; test if string is	aligned	on 32 bits
	je	short main_loop_entrance

src_misaligned:			    ; simple byte loop until string is aligned
	mov	dl,byte	ptr [ecx]
	add	ecx,1
	test	dl,dl
	je	short byte_0
	mov	[edi],dl
	add	edi,1
	test	ecx,3
	jne	short src_misaligned
	jmp	short main_loop_entrance

main_loop:			    ; edx contains first dword of sorc string
	mov	[edi],edx	    ; store one	more dword
	add	edi,4		    ; kick dest	pointer
main_loop_entrance:
	mov	edx,7efefeffh
	mov	eax,dword ptr [ecx] ; read 4 bytes

	add	edx,eax
	xor	eax,-1

	xor	eax,edx
	mov	edx,[ecx]	    ; it's in cache now

	add	ecx,4		    ; kick dest	pointer
	test	eax,81010100h

	je	short main_loop
	; found	zero byte in the loop
; main_loop_end:
	test	dl,dl		    ; is it byte 0
	je	short byte_0
	test	dh,dh		    ; is it byte 1
	je	short byte_1
	test	edx,00ff0000h	    ; is it byte 2
	je	short byte_2
	test	edx,0ff000000h	    ; is it byte 3
	je	short byte_3
	jmp	short main_loop	    ; taken if bits 24-30 are clear and	bit
				    ; 31 is set
byte_3:
	mov	[edi],edx
	mov	eax,[esp+8]	    ; return in	eax pointer to dest string
	pop	edi
	ret
byte_2:
	mov	[edi],dx
	mov	eax,[esp+8]	    ; return in	eax pointer to dest string
	mov	byte ptr [edi+2],0
	pop	edi
	ret
byte_1:
	mov	[edi],dx
	mov	eax,[esp+8]	    ; return in	eax pointer to dest string
	pop	edi
	ret
byte_0:
	mov	[edi],dl
	mov	eax,[esp+8]	    ; return in	eax pointer to dest string
	pop	edi
	ret

strcat	endp

	end

呢?
赵4老师 2016-01-26
  • 打赏
  • 举报
回复
就算你这次准确猜出了出题者的心意,以后你在出题者手下工作的时候难道天天去猜他的心意吗?!
imGala 2016-01-26
  • 打赏
  • 举报
回复
哈哈哈,挺有意思
fefe82 2016-01-26
  • 打赏
  • 举报
回复
引用 1 楼 oN5GrzoN 的回复:
说起这位面试者,年龄不大,感觉刚毕业的样子(毕竟小公司). 后来问了个问题,把多线程的几个函数背一下.我也说实话,多线程,曾经也没少做,不过有一段时间没做了,函数大概记得的有createThread, terminateThread之类的,听我这么一说,他又是一脸很不满意的样子. 我心说了,平时用这些,不是现搜吗?难道真要背会?
多线程还真不是有一套函数手册就搞的定的。 所以即使把相关函数的用法都背下来也不一定就写的好。
fly_dragon_fly 2016-01-25
  • 打赏
  • 举报
回复
这是故意找茬吧, 要求就两点1)已经完成,2)鬼知道你要怎么用,至于重叠,地址可不可写,传入参数是否有效, 都属于扯蛋的范围, 我接口告诉你不能这样用, 你偏要用, 那应该自己负责
gaodlike 2016-01-22
  • 打赏
  • 举报
回复
他想让你写一个动态规划? 给定两个字符串s1,s2,求变换次数最小的变换。 可用的变换为字符替换,字符平移?
oN5GrzoN 2016-01-22
  • 打赏
  • 举报
回复
说起这位面试者,年龄不大,感觉刚毕业的样子(毕竟小公司). 后来问了个问题,把多线程的几个函数背一下.我也说实话,多线程,曾经也没少做,不过有一段时间没做了,函数大概记得的有createThread, terminateThread之类的,听我这么一说,他又是一脸很不满意的样子. 我心说了,平时用这些,不是现搜吗?难道真要背会?

15,440

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧