home *** CD-ROM | disk | FTP | other *** search
- /*
- // Sample application to test dynamic memory allocation by randomly
- // allocated and freeing random sized blocks of memory. This demo
- // requires the Paradigm MS-DOS emulator to be present.
- //
- // Memory management takes place on the heap for small data memory
- // models and on the far heap for large data models.
- */
-
- #include <stddef.h>
- #include <stdlib.h>
- #include <alloc.h>
- #include <mem.h>
-
- #define ALLOCS 250
- #define BLOCK_SIZE 100
-
- char *p[ALLOCS] ;
-
-
- void main(void)
- {
- unsigned int item ;
- unsigned int size ;
- unsigned long allocs ;
- unsigned int errors ;
-
- allocs = errors = 0 ;
- while (1) {
- /* Compute the pointer and allocation size */
- item = random(ALLOCS) ;
- size = random(BLOCK_SIZE) + 1 ;
-
- /* Free up the memory if in use */
- if (p[item] != NULL)
- free(p[item]) ;
-
- /* Allocate a new block */
- if ((p[item] = malloc(size)) == NULL)
- errors++ ;
- else {
- allocs++ ;
- memset(p[item], item, size) ;
- }
- }
- }
-