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