Save this video free

Lecture 26: Memory allocation (cont); A custom string type

Burton Ma · 50:00 · Watch on YouTube

Part of Queen's University CISC220. See all 23 lectures and search them

These notes were made with YouTube Collector. Paste any YouTube link and get the same: a summary, chapters and a searchable transcript.

Overview

Burton Ma explains C dynamic memory allocation with `calloc`, `realloc`, and `free`, then applies those tools to building a mutable string type. The design stores a character pointer, string length, and buffer capacity in a struct, using constructor-like functions to allocate and copy strings while highlighting null terminators, ownership, and object lifetimes.

Key takeaways

Chapters

0:00 Using calloc to Allocate Zero-Initialized Memory
5:00 How realloc Resizes Dynamically Allocated Blocks
10:00 Growing a C String with realloc
15:00 Freeing Allocations and the Limits of C Strings
21:00 Designing a Mutable String Struct with Length and Capacity
25:00 Manually Allocating and Using the Custom String
31:00 Avoiding Invalid Buffer Lifetimes with Constructor Functions
35:00 Implementing a Constructor for an Empty String
41:00 Copying a C String into the Custom String Type
46:00 Copying an Existing String Struct

Keep these chapters and the full searchable transcript in your own library.

Transcript excerpt

0:01 All right. Good afternoon, everybody. So we're just going to pick up from where we left off in the last class. So remember in the last class, we were talking about dynamic memory allocation. Uh and we saw how to use malloc. Right? So remember malloc lets you allocate a block of memory. You specify how big a block of memory you want. So if you want to store n items of some size, right? You call malloc with n times size of. There's a closely related function called calloc. Uh now malloc, you can probably guess stands for memory allocation, which is probably true. Calloc, no one actually remembers what the c is for. Um but it's called calloc. The way you use it is slightly different. Um you ask for you tell calloc how many items you want.

0:50 So here I want n items and you tell it how big each item is. So calloc will do this multiplication for you. Right? Calloc also has another side effect. Uh so uh it sets all the bits of the allocated memory to zero. Right? Malloc is uh uninitialized. And Whoops. Uninitialized. Right? So malloc gives you back a chunk of memory. There could be something in that memory already. Calloc sets everything to zero. Uh so what this means is that um if you treat

1:37 uh the memory returned by calloc as an array of some kind of integer type, all of your ints are zero. If it's a floating point type, they're all equal to 0.0. Uh and if they're a character array, then all of your strings are the empty strings. Right? Sorry, then your string is the empty string. Right? Because it's set to zero, that's like having the null terminator in the first uh element of the array, which is

This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.

In this course

Related notes

Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Burton Ma.

Want the full transcript?

Save this video in YouTube Collector to get its complete searchable transcript, your own AI summaries, and a library that keeps every video you collect in one place.