某外企c语言笔试题。。 求指导

sulemon 2004-11-17 10:35:49
1、作为开发团队的一员,你需要实现一些库函数提供给其他人使用。假设你实现的一个
函数原型如下:
int DoSomeThing(char* pParam)
{
...
}


2、下面的代码有什么问题?
char *_strdup( const char *strSource )
{
static char str[MAX_STR_LEN];
strcpy(str, strSource);
return str;
}


3、实现一个函数:取出一个全路径文件名中的全路径。
/* [in] pszFullPath 全路径文件名
[out] pszPathName 接收全路径的缓冲区
[out] nCount 缓冲区大小
*/
int ExtractFilePath(char* pszFullPath, char* pszPathName, int nCount)
{
...
}
...全文
1684 26 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
#define MAX_STR_LEN 100

#include <iostream>

#include <stdio.h>

//using namespace std;

char *_strdup(const char *s )
{
/* static char str[MAX_STR_LEN];
strcpy(str, strSource);
return str;*/
const char *_old = s;
int _len = strlen(s);
char*_new=(char *)malloc(_len+1);
_new[_len]='\0';
memcpy(_new,_old,_len);
return _new;
}

int main(void)
{
char *hl;
char *wld;
hl = _strdup("hello");
wld = _strdup(",world");

printf("%s%s\n",hl,wld);

return 0;
}
victorchen_2000 2005-03-04
  • 打赏
  • 举报
回复
这个 strdup 根本就是错误的。
char * strdup(char *s)
{
char *__old=(s);
int __len = strlen(s);
char*__new=(char *)malloc(__len+1);
__new[__len]='\0';
memcpy(__new,__old,__len);
return __new;
}

xxxsunzk 2004-11-19
  • 打赏
  • 举报
回复
知道了
局部静态变量只在定义它的函数中有效

xxxsunzk 2004-11-19
  • 打赏
  • 举报
回复
为何 数组str[MAX_STR_LEN]在函数返回后就不存在了请教一下
evilch 2004-11-19
  • 打赏
  • 举报
回复
2、下面的代码有什么问题?
char *_strdup( const char *strSource )
{
static char str[MAX_STR_LEN];
strcpy(str, strSource);
return str;
}


这是个sb问题
lbaby 2004-11-19
  • 打赏
  • 举报
回复
同意神经病的说法,也许我们换种说法更好一点:
char *_strdup( const char *strSource )
{
static char str[MAX_STR_LEN];
strcpy(str, strSource);
return str;
}
上边的代码问题有二:
1.str的缓冲区溢出
2.str是静态成员
静态成员带来的问题是:
它是本进程内唯一的,
它的生存周期是全局的,但是只有局部可见
在以上的代码中
static char str[MAX_STR_LEN];只在初次_strdup时被初始化
以后每次执行,其实是执行的:
char *_strdup( const char *strSource )
{
strcpy(str, strSource);
return str;
}
也就是说,不管是本进程的哪个线程调用这个函数,
都是对同一个内存地址在进行存取


以下的代码输出是什么?,为什么会这样?

#define MAX_STR_LEN 100

char *_strdup( const char *strSource )
{
static char str[MAX_STR_LEN];
strcpy(str, strSource);
return str;
}

#include <stdio.h>

int main(void)
{
char *hl;
char *wld;
hl = _strdup("hello");
wld = _strdup(",world");

printf("%s%s",hl,wld);

return 0;
}


人家只问你上边的代码会有什么问题,并没有让你改正

flyingbugs 2004-11-19
  • 打赏
  • 举报
回复
xxxsunzk(大哥) ( ) 信誉:100 2004-11-19 10:19:00 得分: 0


知道了
局部静态变量只在定义它的函数中有效




---------------------------------
static局部变量是可以把指针返回的!
局部静态变量所谓的局部只是说这个变量本身也就是句柄是局部的,
其指针的值是可以返回的。

而且最重要的一点:只有static的这种数组才能返回,因为,static数组在不是在函数调用堆栈中的,所以不会随着函数的返回而被释放(pop)掉,
那个_strdup函数 主要问题是:线程不安全和没有检查长度!
如果要线程安全必须:
char * str=malloc(MAX_STR_LEN*sizeof(char));
.....
这样str的内存是分配在heap(堆)上,函数返回不会被释放!
bailindf2 2004-11-19
  • 打赏
  • 举报
回复
const在这有什么用,谁能讲讲?:)
xiaozhong 2004-11-18
  • 打赏
  • 举报
回复
上面的朋友说的很有道理,题2主要考虑溢出和地址返回的问题!
sbli 2004-11-18
  • 打赏
  • 举报
回复
up
mofei1985 2004-11-18
  • 打赏
  • 举报
回复
学习 UP
skyxie 2004-11-17
  • 打赏
  • 举报
回复
第二个问题:
3.提示调用者传入该函数的字符指针的长度不可大于MAX_STR_LEN-1
skyxie 2004-11-17
  • 打赏
  • 举报
回复
第二个问题:
0.用static是必须的,否则就会出现
houdy(致力于图像/图形领域,成为有思想的程序员) ( ) 回复的第一点
1.char *_strdup( const char *strSource )
{
static char str[MAX_STR_LEN];
//strcpy(str, strSource); //上面已有说明缓冲区溢出问题
//因此将此句改为:
strncpy(str, strSource, MAX_STR_LEN);
return str;
2.提示调用者接收该函数传出的指针后不可free or delete.
比如调用者这样调用肯定出错
const char *pSrc = "....";
char *p = _strdup( pSrc );
//use p
free(p);//肯定错
// or delete p; //肯定错
}
ywfscu 2004-11-17
  • 打赏
  • 举报
回复
第1个:改为int DoSomeThing(const char* pParam)
第2个:可能缓冲区溢出,确保输入字符串末尾包含'\0'是函数使用者的事情,不是这个函数的责任,
第3个:从右向左搜索第一个 '\' 字符
houdy 2004-11-17
  • 打赏
  • 举报
回复
第二个问题:
1.数组str[MAX_STR_LEN]在函数返回后就不存在了,任何对此内存区域内的操作都是危险的。
2.除此之外,strlen(strSource)+1>MAX_STR_LEN,就会出现overflow的问题,很危险。
songmaohai 2004-11-17
  • 打赏
  • 举报
回复
第二题的目的是考“缓冲区溢出”吧?
toyjoy 2004-11-17
  • 打赏
  • 举报
回复
3. 从右向左搜索第一个 '\' 字符
lxjlz 2004-11-17
  • 打赏
  • 举报
回复
up
liloaka 2004-11-17
  • 打赏
  • 举报
回复
在strcpy(str, strSource)之前,应该先
int n = strlen(strSource);
if(n < MAX_STR_LEN)
strcpy(str, strSource);
Scorpio_Shooter 2004-11-17
  • 打赏
  • 举报
回复
static char str[MAX_STR_LEN];应改为char str[MAX_STR_LEN];]
加载更多回复(6)

70,020

社区成员

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

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