设为首页 加入收藏

TOP

C语言基础测试题解析(二)
2018-04-25 06:05:57 】 浏览:488
Tags:语言基础 测试题 解析
)malloc(num); } int main() { char *str = NULL; GetMemory(&str, 100); strcpy(str, "hello"); printf(str); printf("\n"); system("pause"); return 0; }

运行结果:
这里写图片描述

答:能输出结果,但是会出现内存泄露。

17、请问运行下面代码会出现什么样的后果?为什么?

#include
    
     
#include
     
       int main() { char *str = (char*)malloc(100); strcpy(str, "hello"); free(str); if (str != NULL) { strcpy(str, "world"); printf(str); } printf("\n"); system("pause"); return 0; }
     
    

运行结果:

答:程序会出现异常,存在野指针,因为释放动态内存后没有置空,if语句不起作用。

注意:malloc要和free同用。

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇面试题:C语言程序必须从main函数.. 下一篇C语言的单引号和双引号实例讲解

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目