You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

31 lines
656 B
C

#include <stdio.h>
#include <stdlib.h>
int main(void){
int o = 1337;
printf("Let's learn to use malloc!\n");
int *p = malloc(sizeof(*p));
/* We need to make sure we got the memory block*/
if (p){
fprintf(stdout, "Ok we got a good value from malloc we can assign a value.\n");
}
else {
fprintf(stderr, "Well fuck this didn't work. Malloc failed");
return 1;
}
printf("Ok the address of o is on the stack at: %p\n", &o);
printf("*p is on the heap at: %p\n", p);
printf("Let's assign a value to p: \n");
scanf("%d", p);
printf("We got %d\n", *p);
printf("Freeing up the memory\nClosing program...\n");
free(p);
p = 0;
return 0;
}