VS2010中malloc的使用

在VS2010 Express中,寫c程序:

例子1:

#include <stdio.h>
#include <stdlib.h>

#define LEN 128

int main()
{
	printf("\n");
	int* a = (int*)malloc(sizeof(int)*LEN);
	free(a);
	return 0;
}

編譯失敗,報錯:

1>------ Build started: Project: MyCpp, Configuration: Debug Win32 ------
1>  test_malloc.c
1>d:\myprojects\mycpp\poj\test_malloc.c(9): error C2143: syntax error : missing ';' before 'type'
1>d:\myprojects\mycpp\poj\test_malloc.c(10): error C2065: 'a' : undeclared identifier
1>d:\myprojects\mycpp\poj\test_malloc.c(10): warning C4022: 'free' : pointer mismatch for actual parameter 1
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


例子2:

#include <stdio.h>
#include <stdlib.h>

#define LEN 128

int main()
{
	int* a;
	printf("\n");
	a = (int*)malloc(sizeof(int)*LEN);
	free(a);
	return 0;
}

卻可以編譯成功。


但用gcc編譯,例子1和例子2卻都可以成功。具體原因不知道。使用的時候小心一些吧。


發佈了140 篇原創文章 · 獲贊 18 · 訪問量 79萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章