Ticket #3183: memory.c

File memory.c, 522 bytes (added by Jixt, 15 years ago)

Memory leak example

Line 
1
2#include <stdlib.h>
3#include <stdio.h>
4
5int main()
6{
7 unsigned char *data;
8 char str[] = "This is a memory test";
9 int i;
10 unsigned long curoff = 0;
11
12 printf("Allocating memory ...\n");
13
14 while(1)
15 {
16 data = malloc(strlen(str)+1);
17 memcpy(data,str, strlen(str) + 1);
18 curoff = curoff + strlen(str) + 1;
19 for(i = 0; i < 40; i++)
20 {
21 data = realloc(data, curoff + strlen(str) + 1);
22 memcpy(data + curoff,str,strlen(str) + 1);
23 curoff = curoff + strlen(str) + 1;
24 }
25 free(data);
26 data = NULL;
27 }
28 return 0;
29}