home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / cplus / 16009 < prev    next >
Encoding:
Text File  |  1992-11-09  |  3.8 KB  |  135 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!munnari.oz.au!metro!mama!news
  3. From: colin@cole.research.canon.oz.au (Colin Pickup)
  4. Subject: Re: Using 64k objects
  5. Message-ID: <BxH4p6.8Bn@research.canon.oz.au>
  6. Sender: news@research.canon.oz.au
  7. Organization: Canon Information Systems Research Australia
  8. References: <1992Nov4.011604.5884@piccolo.cit.cornell.edu>
  9. Date: Tue, 10 Nov 1992 00:15:05 GMT
  10. Lines: 123
  11.  
  12. In article <1992Nov4.011604.5884@piccolo.cit.cornell.edu>  
  13. sl14@crux3.cit.cornell.edu (Stephen Lee) writes:
  14. > I'm writing a program which allocates a large array of unsigned int.
  15. > I'm passing the array to a function which then manipulates it.  No
  16. > problem until I tried a large number (32768):  The function seems to
  17. > be overwriting its variables.  I can use an array of 16384 elements
  18. > and get the correct result.
  19. > So, is there any restrictions using large arrays on a PC?  The problem
  20. > seems to be caused by the array crossing a segment boundry:  32768
  21. > ints are 64k in size.
  22. > The compiler I'm using is BC++ 2.0, and I tried compiling with
  23. > align byte or align word, stack checking on/off, and large/small
  24. > memory model.  In small model it overwrites its variables.  In large
  25. > model it doesn't, but it screws up memory and 4DOS barfs about error
  26. > allocating memory when the program exits.
  27. > The program is as follows:
  28. > Note:  word is typedef'd as unsigned int.
  29. >     The function is fft_idx, which stops in the middle of the 
  30. >     #ifdef DEBUG block, at i = 14202 (or near that) 
  31. > // fft.cpp: Fast Fourier Transform
  32. > #include <stdlib.h>
  33. > #include <math.h>
  34. > #include <complex.h>
  35. > #include "auxtypes.h"
  36. > // pointer equiv. of 0, a.k.a. NULL
  37. > #define NIL 0
  38. > #define DEBUG
  39. > #ifdef DEBUG
  40. > #include <iostream.h>
  41. > #include <iomanip.h>
  42. > #endif
  43. > #define _2_PI 6.28318530717958647692528676655900576839433879875016
  44. > complex _neg_2_pi_i = complex(0, -_2_PI);
  45. > void fft_idx(word nmax, word *idx)
  46. > /*
  47. >  * Calculate index for the elements in the final butterfly arrangement.
  48. >  */
  49. > {
  50. >   word i, n2;
  51. >   unsigned long n;  // screw up in the for loop if word is used:
  52. >                     // becomes 0 after rolling off 32768, n <= nmax  
  53. always
  54. >   idx[0] = 0;
  55. >   for (n = 2L; n <= nmax; n <<= 1) {
  56. >     n2 = (word) n>>1;
  57. >     for (i = 0; i < n2; i++) {
  58. >       idx[i] <<= 1;
  59. >       idx[i+n2] = idx[i] + 1;
  60. > #ifdef DEBUG
  61. >       if (n2 == 16384 && n != 32768L) {
  62. >         cout <<"n : "<<n<<"\ni: "<<i<<"\nn2: "<<n2<<"\n";
  63. >         exit(1);
  64. >       }
  65. > #endif
  66. >     }
  67. > #ifdef DEBUG
  68. >     cout << n << ", " << n2 << endl;
  69. > #endif
  70. >   }
  71. > }
  72. > complex *fft(word n, complex *&in, complex *&out)
  73. > /*
  74. >  * Do a FFT on the complex array 'in' of size 'n'
  75. >  * Returns the address of 'out'
  76. >  * The array 'in' is scrambled, and pointer 'in' may also swap with  
  77. 'out'
  78. >  *
  79. >  * 'n' is expected to be a power of 2, and no bigger than 32768
  80. >  * (or whatever power of 2 an unsigned int can hold)
  81. >  */
  82. > {
  83. >   word *idx;
  84. >   word i;
  85. >   if ((idx = new word[n]) == NIL)
  86. >     return NIL;
  87. >   fft_idx(n, idx);
  88. > #ifdef DEBUG
  89. >   for (i = 0; i < n; i++)
  90. >     cout << setw(7) << idx[i] << ",";
  91. >   cout << endl;
  92. > #endif
  93. > }
  94. > #ifdef DEBUG
  95. > void main()
  96. > {
  97. >   complex *in, *out;
  98. >   word n;
  99. >   cerr << "n?\n";
  100. >   cin >> n;
  101. >   fft(n, in, out);
  102. > }
  103. > #endif
  104. > --
  105. > Stephen Lee
  106. > Internet: sl14@crux2.cit.cornell.edu
  107.  
  108. Borland is one of the few C/C++ compilers that handles arrays bigger that  
  109. 64K. To do it either use the huge memory model for the whole program or  
  110. just make the pointers to the arrays huge (read the manuals on how to do  
  111. this). The second option will give better run-time performance.
  112.  
  113. NOTE : you can not use new to allocate arrays bigger than 64K. The size  
  114. parameter for new is a unsigned, i.e. 16 bits. You must use hugealloc and  
  115. hugefree (again look in the manual for these functions).
  116. Colin Pickup
  117.  
  118.