Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <malloc.h>
- struct s{
- int num;
- };
- int main(){
- struct s *a,*b,*c,*d,*e,*f;
- a = malloc(sizeof(struct s));
- printf("a is at %p \n",a);
- b = malloc(sizeof(struct s));
- printf("b is at %p \n",b);
- c = malloc(sizeof(struct s));
- printf("c is at %p \n",c);
- free(a);
- printf("Freeing a : %p \n",a);
- free(c);
- printf("Freeing c : %p \n",c);
- free(b);
- printf("Freeing b : %p \n",b); //Bypassing top check
- free(c);
- printf("Freeing c : %p again \n",c); //Double free
- printf("The freed bins will be added to the corresponding bins \n");
- printf("Now we are again allocating memory using malloc into three pointers d,e and f \n");
- printf("As they are of the same sizes as the previously allocated chunks, pointer d,e and f will get allocated the chunks that have been just free()d and added to the fastbin earlier \n ");
- d = malloc(sizeof(struct s));
- printf("d is at %p \n",d);
- e = malloc(sizeof(struct s));
- printf("e is at %p \n",e);
- f = malloc(sizeof(struct s));
- printf("f is at %p \n",f);
- d->num = 1111;
- printf("d->num is assigned to 1111 \n");
- f->num = 2222;
- printf("f->num is assigned to 2222 \n");
- printf("Now let's print the values of the both e->num and d->num \n");
- printf("d->num is %d \n ",d->num);
- printf("f->num is %d \n ",f->num);
- }
Advertisement
Add Comment
Please, Sign In to add comment