example

Link to this paste: http://bugs.dragonflybsd.org/pastes/29

Added by Anonymous about 3 years ago.
Syntax: Plain Text

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <stdio.h>
#include <stdlib.h>

struct nums {
        int a;
        int b;
};

void
print(struct nums *p, int len)
{
        int i;
        for (i = 0; i < len; i++) {
                fprintf(stdout, "a=%d b=%d\n", p[i].a, p[i].b);
        }
}

int
main(void)
{
        struct nums *p, *tmp;
        int i;
        int len = 4;

        p = malloc(len * sizeof(*p));

        for (i = 0; i < 4; i++) {
                p[i].a = i * 2;
                p[i].b = p[i].a - 1;
        }

        print(p, len);

        len += 4;
        if ((tmp = realloc(p, len)) != NULL) {

                if (p)
                        free(p);
                else
                        printf("Dude...\n");
                p = NULL;
        }

        p = tmp;
        print(p, len);

        return 0;
}


---

Result:

> ./t_rl
Allocating 32 bytes
a=0 b=-1
a=2 b=1
a=4 b=3
a=6 b=5
assertion: (*bmp & (1LU << bno)) == 0 in memfree
Abort (core dumped)

Download