[问题] 函数原型宣告造成内存问题

楼主: yshihyu (yshihyu)   2016-11-25 00:24:36
开发平台(Platform): (Ex: Win10, Linux, ...)
stm32 freertos
编译器(Ex: GCC, clang, VC++...)+目标环境(跟开发平台不同的话需列出)
IAR
#include <stdio.h>
int main(int argc, char *argv[])
{
int a[4] = {0};
printf("main=%p\n",(void*)a);
test(a);
return 0;
}
void test(int *a)
{
printf("test=%p\n",(void*)a);
}
warning: implicit declaration of function ‘test’
[-Wimplicit-function-declaration]
我今天在stm32 freertos 写程式没加上函数原型宣告
程式类似上面这样印出内存位址结果竟然不一样, test 印出来是一块不合法位址 access 会造成crash
后来我看编译器有提醒没加函数原型加上后, 两边印出来内存位址都一样
在程式在pc 上没问题~好奇为什么在STM32 freertos / IAR 编译器跑出来会有这样情况?
我理解的是函数原型在检查呼叫传入参数型态个数跟定义函数是否有一致~
不一致编译器会报 warning
可是我就算没宣告原型应该不至于test函数会印出不一样位址, 因为我两边型态都是 int*
请问有人知道这是什么原因造成 test 函数印出来位址跟 main 函数的 a 阵列位址是不相同
谢谢
作者: Bencrie   2016-11-25 00:45:00
函数默认原型是 void func(int)intint 跟 int* 不见得一样长
楼主: yshihyu (yshihyu)   2016-11-25 00:50:00
我想起来有默认原型xd...感谢你提醒

Links booklink

Contact Us: admin [ a t ] ucptt.com