C, Memory

CS 2130: Computer Systems and Organization 1

April 10, 2026

Announcements

The Heap

The heap: unorganized memory for our data

The Heap: Requesting Memory

void *malloc(size_t size);

Java

What is the closest thing to malloc in Java?

malloc Example

typedef struct student_s {
    const char *name;
    int credits;
} student;

student *enroll(const char *name, int transfer_credits) {
    student *ans = (student *)malloc(sizeof(student));
    ans->name = name;
    ans->credits = transfer_credits;
    return ans;
}

The Heap: Freeing Memory

Freeing memory: free

void free(void *ptr);

Garbage

Garbage - memory on the heap our code will never use again

Garbage Collector

Garbage Collector - frees garbage “automatically”

malloc man page

calloc, realloc

Common Memory Bugs (reading)

List example