Turbo C記憶體問題

Home Home
引用 | 編輯 IPIS
2007-04-19 15:23
樓主
推文 x0
Turbo C記憶體問題

01 #include <stdio.h>
02 #include <conio.h>
02 #include <dos.h>
04 #include <float.h>
05 #include <math.h>
06 #include <alloc.h>
07 void main(void)
08 {
09 long fori=0,nums=3000;
10 double far *bigarray;
11 if ((bigarray=farmalloc(nums*sizeof(double)))==NULL)
12 {
13 printf("sbrk failed.\n");
14 getch();
15 exit(0);
16 }

17 for (fori=0;fori<100;fori++)
18 *(bigarray+fori)=0.01*fori;


19 for (fori=0;fori<100;fori++)
20 printf("%3.7f\n",*(bigarray+fori));

21 getch();
22 farfree(bigarray);
23 free(bigarray);
24 getch();
25 farfree((float *)bigarray);
26 free((float *)bigarray);
27 } ..

訪客只能看到部份內容,免費 加入會員



獻花 x0
引用 | 編輯 GNUGCC
2007-04-25 01:21
1樓
  
配置記憶體沒問題,問題出在你是否有將編譯器的記憶體使用模式改成 Large 或是 Huge ?
因為如果沒有改的話這些呼叫會沒作用因為編譯器預設記憶體使用模式是在 64 K 範圍內,超過當然會出現錯誤。

這個敘述 :
if ((bigarray=farmalloc(nums*sizeof(double)))==NULL)

最好改成
if ((bigarray=(double far*)farmalloc(nums*sizeof(double)))==NULL)

因為 farmalloc() 回傳的是 void far*, 有些編譯器會強制轉換成對應的型態為了相容性最好養成這樣的習慣


這個敘述好像有問題,原本的才是對的
17new for (fori=0;fori<100;fori++)
18new *(bigarray+(fori*sizeof(double)))=0.01*fori;
19new for (fori=0;fori<100;fori++)
20new printf("%3.7f\n",*(bigarray+(fori*sizeof(double))));


這個有問題 :
22 farfree(bigarray);
23 free(bigarray);
24 getch();
25 farfree((float *)bigarray);
26 free((float *)bigarray);
27 }

釋放記憶體只要呼叫 farfree 一次就夠了, 不用再呼叫 free, 就算呼叫 free 也沒有作用.

獻花 x0