微软招聘题,大家进来讨论一下(非高手莫入)

Regen219 2003-06-20 09:23:43
五、编写strcpy函数(10分)
已知strcpy函数的原型是
char *strcpy(char *strDest, const char *strSrc);
其中strDest是目的字符串,strSrc是源字符串。
(1)不调用C++/C的字符串库函数,请编写函数 strcpy
(2)strcpy能把strSrc的内容复制到strDest,为什么还要char * 类型的返回值?

我的答案如下:
(1)
char *strcpy(char *strDest, const char *strSrc);
{
assert((strDest!=NULL) && (strSrc !=NULL));
char *address = strDest;
while( (*strDest++ = * strSrc++) != ‘\0’ )
NULL ;
return address ;
}
(2)为了实现链式表达式

大家看看我的答案对不对,如果不对,写出你们的答案。
...全文
99 149 打赏 收藏 转发到动态 举报
写回复
用AI写文章
149 条回复
切换为时间正序
请发表友善的回复…
发表回复
yaoxinyan 2003-07-04
  • 打赏
  • 举报
回复
TO: wingfiring(别逗了)

你究竟明不明白什么叫DEBUG版,什么叫RELEASE版啊?老大……

看这个弱智的例子:

void fun()
{
#ifdef DEBUG
assert(...)
#endif

xxxxxxxxxxxx;
yyyyyyyyyyyy;
zzzzzzzzzzzz;
return ;
}

当你DEBUG的时候,定义宏DEBUG,assert自然就起作用了;当你RELEASE的时候,由于无定义DEBUG,assert就隐形啦!
我们争论的是,在RELEASE的时候要不要仍然把assert保留!至于在写DEBUG版的时候你喜欢用assert那是你的爱好。

唉呀,这么基础的东西都不懂别在这献丑啦,拜托!!!
alphawin 2003-07-04
  • 打赏
  • 举报
回复
太乱了,本想来看看技术,废话看了一箩筐。
xundeng 2003-07-03
  • 打赏
  • 举报
回复
我总觉得我写得程序有不尽人意的地方,原来,这么简单的,也这么复杂 ,
如果上天给我一个机重来一次的话,我 。。。。。一定不做程序员了
budzhang 2003-07-03
  • 打赏
  • 举报
回复
又:说到效率,我认为不能不提到:

效率是否有问题,要看参照物而言。

那么,c的库函数的设计者的参照物(在效率方面)是什么哪?
budzhang 2003-07-03
  • 打赏
  • 举报
回复
又,不是专牛角尖:

但是说到效率,请说明参照物是什么!

具体而言,c语言库作者的参照物是什么?
budzhang 2003-07-03
  • 打赏
  • 举报
回复
谈到程序的健壮性,请各位到
“高手请进-----讨论一下一个7x24小时运行的服务程序的特征”
中论述一下具体的措施。
wingfiring 2003-07-03
  • 打赏
  • 举报
回复
> yaoxinyan() ( )

TO: wingfiring(别逗了)

从一开始你就没有理解我们这里讨论的核心,我们争论的是库代码需不需要对"用户传入的参数"进行assert检测;而不是你所理解的"写库代码(的debug版本)时需不需要用assert去帮助查错".

对于前者,很清楚,没有必要!
对于后者,很清楚,看具体情况.


看来你是认为应该开发的时候是一套源代码,发布的时候是另一套源代码喽?库代码也是要逐渐开发出来的,而我认为“写库代码(的debug版本)”就是我们要发布的版本,不应该对源代码处理过以后再加以发布。我认为如果没有充分的理由,这种人为的修饰起码是不可靠的行为,甚至是不诚实的,觉不是严谨的开发态度。
Octogen 2003-07-03
  • 打赏
  • 举报
回复
看看微软的CRT是如何实现的:)
个人认为这种基础函数不用加什么防御,不然很容易成为热点。

page ,132
title strcat - concatenate (append) one string to another
;***
;strcat.asm - contains strcat() and strcpy() routines
;
; Copyright (c) 1985-1997, 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
push edi ; preserve edi
mov edi,[esp+8] ; edi points to dest string
jmp short copy_start

strcpy endp

align 16

strcat proc

.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]
inc ecx
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]
inc ecx
test dl,dl
je short byte_0
mov [edi],dl
inc edi
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
armstrong119 2003-07-03
  • 打赏
  • 举报
回复
(1)不调用C++/C的字符串库函数,请编写函数 strcpy
char *strcpy(char *strDest, const char *strSrc);
{
assert((strDest!=NULL) && (strSrc !=NULL));
char *address = strDest;
while( (*strDest++ = * strSrc++) != ‘\0’ )
NULL ;
return address ; }

(2)strcpy能把strSrc的内容复制到strDest,为什么还要char * 类型的返回值?
答:为了实现链式表达式。
int length = strlen( strcpy( strDest, “hello world”) );
wangzi31323 2003-07-03
  • 打赏
  • 举报
回复
胜读半天书
yaoxinyan 2003-07-03
  • 打赏
  • 举报
回复
TO: wingfiring(别逗了)

从一开始你就没有理解我们这里讨论的核心,我们争论的是库代码需不需要对"用户传入的参数"进行assert检测;而不是你所理解的"写库代码(的debug版本)时需不需要用assert去帮助查错".

对于前者,很清楚,没有必要!
对于后者,很清楚,看具体情况.
zteliubin 2003-07-02
  • 打赏
  • 举报
回复
呵呵,看来我成了众矢之的!
各位别生气,库函数不加就算了,我写程序时注意点好了!

本身不就这个问题再发表看法,不过这里大家只是讨论,不要
谩骂和人身攻击,我素质不太高,不能大家也和我一样啊!

呵呵!
zteliubin 2003-07-02
  • 打赏
  • 举报
回复
Windows的复杂是因为它的易用性,说水平低未免太狂妄!
包括你我在内的中国人花几年时间是做不出来的。
微软有时是比较讨厌,不过Windows在推动世界的信息化方面
却是居功至伟,这一点我想记入历史也不为过!
不要因为自己做不出来就说别人水平低嘛,这是什么态度!

至于软件工程问题,说得有些道理!
不过国内的99%的程序员也就只能做这样的事!
看不来业界有些浮躁的风气,好象程序员们都在写操作系统,
都在搞创新设计,而且微软那个Windows算个什么东东嘛,赶明儿个
我就写一个出来! 自己读了点源代码好像就会写操作系统了,
都是顶尖高手,都不会出现野指针之类的问题。
看大虾们都这样写,那我也这样写罗!

这里灌水的也没有几个有资格也写库函数,库函数代码里可以学习一些
技巧,但对于绝大多数人而言,绝不是用来模仿风格的.

库函数加不加assert(),都有道理,不加也不能成为你在程序里不加的理由吧!
等到你有机会写库函数的时候,再考虑这个问题吧!
yaoxinyan 2003-07-02
  • 打赏
  • 举报
回复
对!真正的C库函数是不提供异常保护的!!!!!!!明白没有?
你的理解力实在让我厌恶!
我什么时候说程序员不用assert??????
我说:C库函数是不会用assert去作无谓的NULL指针检测的!!!!!!!
烦!
跟一个死不认输老是跑题的人争论真没意思!!!
算,你老大,你最酷,我不说了。
budzhang 2003-07-02
  • 打赏
  • 举报
回复
对于软件工程,在下的观点是:

软件工程是给代码工厂制定的,不是给业界创新者制定的。

c的库设计是创新者的事情,不是代码工厂的事情。

budzhang 2003-07-02
  • 打赏
  • 举报
回复
windows系统是很复杂。但是为什么复杂的东西就是水平高哪?

我认为复杂的东西正表明了水平低,而不是高。



zteliubin 2003-07-02
  • 打赏
  • 举报
回复
呵呵,
LINUX本身也是由玩家们搞起来的嘛!不过,现在很多大厂家开始重视,
linux的发展并不代表它的代码非常好,而是由很多因素,比如开发源代码,
类unix,微软的垄断等等等等,我非常尊重linux,也用linux,发展前景也看好,
说linux比windows好,未必,微软的垄断,所以遭到大多数人的抵制,Windows本身
也有很多漏洞,但是如果要开发一个Windows操作系统,难度决不在开发一个
linux操作系统之下,Windows操作系统的复杂程度,应该比你想象的大得多。

这里讨论的不是高手应该怎么做的问题,你看看很多高手的源代码,包括操作系统代码,
没有注释,风格好像也不值得恭维,但是作者却是真正高手,而代码也非常好。
那我们一定就要学习他们的这种风格吗,我贴子的本意是作为一个程序员,
也许应该更加注重程序健壮性,现在的大项目,可能是由几百,上千人
来开发,如果都不去判断参数和异常情况处理的话,后果可想而知!

以前曾经看过这样的贴子,并得到很多共鸣:
1。真正的程序员是不写注释的
2。真正的程序员是不写文档的
....
真正的程序员是不用写Assert的?!
真正的库函数是不提供异常保护的?!

yaoxinyan 2003-07-02
  • 打赏
  • 举报
回复
“其实真正做大型软件的人,很少有用GNU C的,大多是玩家用的”

不知Linux内核算不算“大型软件”呢?

按照你的逻辑,那我同样可以说,国内现在搞计算机的很少用Linux平台,用的都是Windows,所以得出结论:Linux比不上Windows。

真正做大型软件的人之所以少用GNU C,是因为他们都局限于某个硬件厂商的平台,例如电信,用的都是SUN、HP的硬件以及专用的UNIX,反正都花了几百万买硬件,也不在乎多花几万买个专用的编译器,最关键的是有商业性质的技术支持,出了问题可以找到主。GNU C虽然好,但没人对它负责,你要用你就必须自己承担所有后果。在国内目前的技术水平上,有几间公司会冒这个风险?所以才很少人用!但这不意味GNU C不好。
凡是总有例外,Apple的创始人,乔布斯,由于MAC X转为使用Darwin内核需要用GNU C编译器,使用之后他对GNU C编译器的质量大为赞赏。老大,乔布斯啊,业界的人公认他为天才人物,他的话总算有点分量吧?

建议上面多位同行,先找几个商业级的C库,反汇编其中的strcpy代码,看看有没有assert再在这里发表宏论!
yaoxinyan 2003-07-02
  • 打赏
  • 举报
回复
那如果我说:SUN,HP,SGI自己的strcpy里也没有assert呢?你还服不服?
zteliubin 2003-07-02
  • 打赏
  • 举报
回复
呵呵,一味高效已经背离了软件工程的思想!
硬件发展很快,所以现在软件更追求稳定,易读!
当然高效是必须考虑的,比如一些平台软件,大型的运算,电信计费,嵌入式系统等!
但这些项目也不是一味追求高效,而是寻求一个平衡!

其实真正做大型软件的人,很少有用GNU C的,大多是玩家用的。
我的说法肯定会招致不少唾沫,不过,GNU C是不错,不过,也没有必要
捧为圣经吧!
加载更多回复(129)

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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