面试中碰到的一个malloc和free的题目

crazy1003 2012-12-03 03:08:14
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main ()
{
char * s = (char *)malloc(100);
s = "Hello world!";

free(s);

if(s == NULL)
printf("Hello\n");
else
printf("no\n");

return 0;
}
问,会输出什么,为什么?
先谢谢了
...全文
542 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
ImportantPerson 2012-12-06
  • 打赏
  • 举报
回复
出错 非法访问 程序退出
MoreWindows 2012-12-04
  • 打赏
  • 举报
回复
s = "Hello world!"; 在这里,s指向了常量数据区,所以后面的free(s)会引发程序非常访问,从而被操作系统结束掉。 关于栈,堆,常量数据区,楼主可以看下我写的《C/C++变量在内存中的分布》 http://blog.csdn.net/morewindows/article/details/6851681
sevk 2012-12-04
  • 打赏
  • 举报
回复
windows xp 强大了。。。
C:\Documents and Settings\Administrator>cat f.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main ()
{
        char * s = (char *)malloc(100);
        s = "Hello world!";
        puts(s);
        printf("%02X\n",s);
        free(s);
        puts(s);
        printf("%02X\n",s);

        if(s == NULL)
                printf("Hello\n");
        else
                printf("no\n");

        return 0;
}


C:\Documents and Settings\Administrator>g++ f.c -o f && f
f.c: 在函数‘int main()’中:
f.c:8:13: 警告:不建议使用从字符串常量到‘char*’的转换 [-Wwrite-strings]
Hello world!
403064
Hello world!
403064
no

C:\Documents and Settings\Administrator>g++ -v
使用内建 specs。
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=d:/mingw/bin/../libexec/gcc/mingw32/4.6.2/lto-wrapper.exe
目标:mingw32
配置为:../gcc-4.6.2/configure --enable-languages=c,c++,ada,fortran,objc,obj-c++
 --disable-sjlj-exceptions --with-dwarf2 --enable-shared --enable-libgomp --disa
ble-win32-registry --enable-libstdcxx-debug --enable-version-specific-runtime-li
bs --build=mingw32 --prefix=/mingw
线程模型:win32
gcc 版本 4.6.2 (GCC)
子善旬 2012-12-04
  • 打赏
  • 举报
回复
面试官拿这种code,是测试脑残的吗???? 在机器上跑下,直接就出问题,很快就能定位了。不值得人工校对。 肯定是国内小公司的面试,就喜欢搞这些无聊的飞机,不如出个算法题,有意义的多。
留翼飞 2012-12-04
  • 打赏
  • 举报
回复
测了一下,什么都输不出,求大神讲解!!!!!
straing 2012-12-04
  • 打赏
  • 举报
回复
通过验证,直接崩了,free之后,S则是一个僵尸指针。
nadleeh 2012-12-04
  • 打赏
  • 举报
回复
输出no, char *s=(char *)malloc(0); 这才爽!
hemmingway 2012-12-04
  • 打赏
  • 举报
回复
这句话就有错误。。

 s = "Hello world!";
xuexingyang 2012-12-04
  • 打赏
  • 举报
回复
脑残面试官+++++
proorck6 2012-12-04
  • 打赏
  • 举报
回复
什么破程序,一个指针指向两个位置,这不出问题才怪呢 具体的运行结果应该是平添相关的吧,不同的编译器会有不同的行为
常如意 2012-12-04
  • 打赏
  • 举报
回复
没必要测试这样的程序
startservice 2012-12-03
  • 打赏
  • 举报
回复
free的时候直接崩掉了。
Binzo 2012-12-03
  • 打赏
  • 举报
回复
"hello world" 字符串的首地址给了s,然后...
_程序员 2012-12-03
  • 打赏
  • 举报
回复
62708807 职业C/C++交流群 以前很火的,后来被群主清空了 现在找人
_程序员 2012-12-03
  • 打赏
  • 举报
回复
会崩溃 你的程序在free(s)之前,指针已经指向一个栈区的内存了,
14号选手 2012-12-03
  • 打赏
  • 举报
回复
程序会崩溃啊 没见过这种写法s="Hello world!"
breakfisher 2012-12-03
  • 打赏
  • 举报
回复
int main ()
{
        char * s = (char *)malloc(100);
        s = "Hello world!"; //s指向一个字符串常量
        free(s); //试图free字符串常量, 会Segmentation fault, 程序终止
        if(s == NULL)
                printf("Hello\n");
        else
                printf("no\n");
        return 0;
}
假如程序是这样的:
char * s = (char *)malloc(100);
        free(s); //free掉s指向的内存空间,但是s并不等于NULL, s还是会指向原来的内存地址,只是那块内存已经free掉了
        if(s == NULL)
          printf("Hello\n");
        else
          printf("no\n"); //最后会输出no
        return 0;
一个比较好的习惯就是每次free(s)后,将s = NULL;
AnYidan 2012-12-03
  • 打赏
  • 举报
回复
引用 3 楼 pengzhixi 的回复:
直接崩溃,释放非堆上的内存。
++
snakedy 2012-12-03
  • 打赏
  • 举报
回复
直接core了,如果溢出到一个可以释放的空间上,也会出"no"的输出
CUMTB_huitailang 2012-12-03
  • 打赏
  • 举报
回复
char * s = (char *)malloc(100); s指向堆中某区域; s = "Hello world!"; s重新指向文字常量区某区域; free(s); 所以程序会奔溃。
加载更多回复(6)

69,387

社区成员

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

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