Memory, Memory Bugs

CS 2130: Computer Systems and Organization 1

April 13, 2026

Announcements

List example

malloc man page

calloc, realloc

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”

Common Memory Bugs (reading)

More on man pages

char *strsep(char **s, const char *d);