File: t1.c

package info (click to toggle)
zmailer 2.99.51.52pre3-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 16,596 kB
  • ctags: 7,422
  • sloc: ansic: 90,470; sh: 3,608; makefile: 2,784; perl: 1,585; python: 115; awk: 22
file content (40 lines) | stat: -rw-r--r-- 925 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <stdio.h>

#define MAXALLOCS   1000
#define SIZE	    50

extern char *sbrk();
extern char *malloc();

main()
{
    char *ptr[MAXALLOCS];
    char *obrk, *nbrk;
    int i;

    obrk = sbrk(0);
    printf("break is initially 0x%x\n", obrk);
    
    for(i = 0; i < MAXALLOCS; i++) {
	ptr[i] = malloc(SIZE);
    }
    nbrk = sbrk(0);
    printf("break is 0x%x (%d bytes sbrked) after %d allocations of %d\n",
     nbrk, nbrk - obrk, MAXALLOCS, SIZE);
    for(i = 0; i < MAXALLOCS; i++) {
	free(ptr[i]);
    }
    nbrk = sbrk(0);
    printf("break is 0x%x (%d bytes sbrked) after freeing all allocations\n",
     nbrk, nbrk - obrk);
    fflush(stdout);

    /* Should be enough memory for this without needing to sbrk */
    (void) malloc(SIZE * (MAXALLOCS / 2));
    nbrk = sbrk(0);
    
    printf("break is 0x%x (%d bytes sbrked) after allocating %d\n",
     nbrk, nbrk - obrk, SIZE * (MAXALLOCS/2));

    exit(0);
}