Let’s take a look at one of the craziest concepts we have in our toolkit to date: dynamically-allocated memory.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int integer_boi = malloc(sizeof(int));
*integer_boi = 123456789;
("integer_boi = %d\n", *integer_boi);
printf
(integer_boi);
free
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
double *pointy_pointer;
= malloc(sizeof(double));
pointy_pointer
*pointy_pointer = 3.14159;
("*pointy_pointer = %lf\n", *pointy_pointer);
printf
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char *letter = NULL;
(sizeof(char));
malloc*letter = 'M';
("The letter of the day is: %c\n", *letter);
printf
(letter);
free
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
float *pi;
= malloc(sizeof(float) * 5);
pi *pi = 3.14;
("pi = %.2lf\n", *pi);
printf
(pi);
free
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char *stringy;
= malloc(sizeof(char) * 30);
stringy (stringy, "goober");
strcpy
for (int i = 0; i < 30; i++)
{
(stringy[i]);
free}
return 0;
}
[1, 2, 3, 4, ..., 20]
.#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int *array;
// Allocate the array
= _____(_____);
array
// Initialize the array
for (_____; _____; _____)
{
[_____] = _____;
array}
// Free the array
;
_____
return 0;
}
typedef struct Computer
{
int memory;
int processes;
float power;
} Computer;
int main(void)
{
*my_PC = malloc(sizeof(Computer));
Computer
("Hello, world!\n");
printf
return 0;
}
true
? What will print out in this
program?typedef struct Computer
{
int memory;
int processes;
float power;
} Computer;
*copy_struct(Computer *pc)
Computer {
*temp = malloc(sizeof(Computer));
Computer ->memory = pc->memory;
temp->processes = pc->processes;
temp->power = pc->power;
temp
return temp;
}
int main(void)
{
*my_PC = malloc(sizeof(Computer));
Computer *my_other_PC;
Computer
->memory = 10000;
my_PC->processes = 29;
my_PC->power = 79.81;
my_PC= copy_struct(my_PC);
my_other_PC
if (my_PC == my_other_PC)
{
("Now my PC and my other PC are equal!\n");
printf}
else
{
("MEM: %d vs %d\n", my_PC->memory, my_other_PC->memory);
printf("PRO: %d vs %d\n", my_PC->processes, my_other_PC->processes);
printf("POW: %.2f vs %.2f\n", my_PC->power, my_other_PC->power);
printf}
(my_PC);
free(my_other_PC);
free
return 0;
}
true
? Also, what is missing from this
program regarding memory management?typedef struct Computer
{
int memory;
int processes;
float power;
} Computer;
*copy_struct(Computer *pc)
Computer {
*temp = pc;
Computer
return temp;
}
int main(void)
{
*my_PC = malloc(sizeof(Computer));
Computer *my_other_PC = copy_struct(my_PC);
Computer
if (my_PC == my_other_PC)
("Now my PC and my other PC are equal!\n");
printf
return 0;
}
typedef struct Computer
{
int memory;
int processes;
float power;
} Computer;
*copy_struct(Computer *pc)
Computer {
*temp = pc;
Computer
return temp;
}
int main(void)
{
*my_PC = malloc(sizeof(Computer));
Computer *my_other_PC = copy_struct(my_PC);
Computer
(my_PC);
free(my_other_PC);
free
return 0;
}
power
for both my_PC
and my_other_PC
?typedef struct Computer
{
int memory;
int processes;
float power;
} Computer;
*copy_struct(Computer *pc)
Computer {
*temp = pc;
Computer ->power = 100;
temp
return temp;
}
int main(void)
{
*my_PC = malloc(sizeof(Computer));
Computer ->power = 79.81;
my_PC*my_other_PC = copy_struct(my_PC);
Computer
(my_PC);
free(my_other_PC);
free
return 0;
}
Pixel
struct with the red, green, and
blue components. (1) Create an array of 1024
pixels
dynamically, (2) assign values to each pixel, and (3)
free the memory associated with the array.typedef struct Pixel
{
int r;
int g;
int b;
} Pixel;
int main(void)
{
*scan_line;
Pixel int n = 1024;
(time(NULL));
srand
// Create an array of pixels. Your code goes here
for (int i = 0; i < n; i++)
{
// Your code goes here
}
// Time to free. Your code goes here
return 0;
}
typedef struct Birthday
{
int day;
int year;
char month[10];
} Birthday;
typedef struct Person
{
char *name;
int age;
;
Birthday birthday} Person;
int main(void)
{
*human = malloc(sizeof(Person));
Person int name_length;
->name = malloc(sizeof(char) * (name_length + 1));
human
// Your code goes here
("Hello, I am %s, a %d-year-old.\n", human->name, human->age);
printf("I was born in %s, %d, %d\n", human->birthday.month, human->birthday.day, human->birthday.year);
printf
return 0;
}
calloc()
typedef struct Computer
{
int memory;
int processes;
float power;
} Computer;
int main(void)
{
Computer *my_PC = calloc(1, sizeof(Computer));
Computer *my_other_PC = malloc(sizeof(Computer));
printf("%d\n", my_PC->memory + my_PC->processes + my_PC->power);
printf("%d\n", my_other_PC->memory + my_other_PC->processes + my_other_PC->power);
free(my_PC);
free(my_other_PC);
return 0;
}