先说下windows
环境:系统Window xp,工具Visual C++ 6.0
代码:
#include <stdio.h>
#include <stdlib.h>
void *malloc(unsigned long size)
{
printf("what a fucking day.\n");
return NULL;
}
int main()
{
char *buf = NULL;
buf = (char *)malloc(10);
if(NULL == buf)
printf("failed.\n");
else
{
printf("%p.\n", buf);
free(buf);
}
return 0;
}
编译
--------------------Configuration: test - Win32 Debug--------------------
Compiling...
test.cpp
D:\VC6\MyProjects\test\test.cpp(15) : error C2668: 'malloc' : ambiguous call to overloaded function
Error executing cl.exe.
test.obj - 1 error(s), 0 warning(s)
编译出错了,完全和我想象中一样,也和俺从老师,书上学到的一样,很好
接下来linux
环境(uname -a)
Linux localhost.localdomain 2.6.18-194.el5 #1 SMP Tue Mar 16 21:52:39 EDT 2010 x86_64 x86_64 x86_64 GNU/Linux
代码(和windows的基本一致)
#include <stdio.h>
#include <stdlib.h>
void *malloc(unsigned long size)
{
printf("what a fucking day.\n");
return NULL;
}
int main()
{
char *buf = NULL;
buf = (char *)malloc(10);
if(NULL == buf)
printf("failed.\n");
else
{
printf("%p.\n", buf);
free(buf);
}
return 0;
}
编译没错,运行结果页没错
[root@localhost test]# gcc -g -Wall -Werror test.c -o test
[root@localhost test]# ./test
what a fucking day.
failed.
[root@localhost test]#
问题来了。。
1 为什么在linux上能编译通过,windows下不行。。编译器,我猜的。
2 在linux下,一个程序中能够存在同名函数,那我怎么知道那个起作用了
有人知道这方面的东西,欢迎指正。。