目前本站已有 十几万 份求职资料啦!


笔试题(GetMemory)

10-16 00:00:09 来源:http://www.qz26.com 笔试题目   阅读:8491
导读:void GetMemory2(char **p, int num){p = (char *)malloc(num); }void Test(void){char *str = NULL;GetMemory(&str, 100);strcpy(str, "hello");printf(str);}请问运行Test函数会有什么样的结果?答:(1)能够输出hello (2 )Test函数中也未对malloc的内存进行释放。(3)GetMemory避免了试题1的问题,传入GetMemory的参数为字符串指针的指针,但是在GetMemory中执行申请内存及赋值语句p = (char *) malloc( num ); 后未判断内存是否申请成功,应加上: if ( *p == NULL ) { ...//进行申请内存失败处理 }void Test(void){char *str = (char *) malloc(100); strcpy(str, “hello&rdqu
笔试题(GetMemory),标签:银行笔试题目,企业笔试题目,http://www.qz26.com

void GetMemory2(char **p, int num)

{

p = (char *)malloc(num);
}

void Test(void)

{

char *str = NULL;

GetMemory(&str, 100);

strcpy(str, "hello");

printf(str);

}

请问运行Test函数会有什么样的结果?

答:

(1)能够输出hello (2 )Test函数中也未对malloc的内存进行释放。(3)GetMemory避免了试题1的问题,传入GetMemory的参数为字符串指针的指针,但是在GetMemory中执行申请内存及赋值语句

p = (char *) malloc( num );
后未判断内存是否申请成功,应加上: if ( *p == NULL ) {

    ...//进行申请内存失败处理
 }

void Test(void)

{

char *str = (char *) malloc(100);

     strcpy(str, “hello”);
     free(str);     
     if(str != NULL)
     {
       strcpy(str, “world”);
printf(str);

}

}

请问运行Test函数会有什么样的结果?

答:执行 char *str = (char *) malloc(100); 后未进行内存是否申请成功的判断;另外,在free(str)后未置str为空,导致可能变成一个“野”指针,应加上: str = NULL;
 


Tag:笔试题目银行笔试题目,企业笔试题目求职笔试面试 - 笔试题目
【字号: 】 【打印】 【关闭
最新更新
推荐热门
联系我们 | 网站地图 | 财务资料 | 范文大全 | 求职简历 | 财会考试 | 成功励志
Copyright 二六求职资料网 All Right Reserved.
1 2 3 4 5 6 7 8 9 10