Save this video free

Lecture 25: Strings (cont); Memory allocation

Burton Ma · 42:14 · 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 how C's `memcpy` and `memmove` copy bytes, then introduces dynamic memory management with `malloc`, `free`, and pointers. He demonstrates allocating memory for individual values and arrays using `sizeof`, returning allocated arrays from functions, and why allocation-size mistakes can appear to work while still causing bugs.

Key takeaways

Chapters

0:00 `memcpy`: Copying Arrays and String Segments by Byte Count
6:00 `memmove`: Shifting Overlapping Array Elements Safely
10:00 `sprintf` and the Transition from String Functions to Memory Allocation
11:30 Allocated Storage Duration and C's Manual Memory Lifecycle
16:00 `malloc` for Individual Values, Structures, and Strings
22:00 Dynamically Allocating Arrays with `sizeof`
29:00 Returning Allocated Arrays and Passing a Pointer to a Pointer
37:00 Checking Allocation Failure and Handling Low Memory

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

Transcript excerpt

0:00 I've jumped ahead a little bit. Uh, so I'm going to skip a few of the functions that are in the notebook. You can read about them on your own time if you want to. Um, they're just basically how to use functions in the in the string header file. Uh, but the two functions at the end of the notebook are more important. So there's the functions memcopy and memmove. Uh, so these are the two functions that you'll want to consider using if you want to copy a chunk of memory uh to another uh location. Neither function moves the memory, right? So neither function actually moves the bits in memory. They both perform a copy. Memcopy is what you want to use if your where you're copying the bits into does not overlap with where you're copying

0:47 them from. So we've seen this function before. Uh, you tell it where do you want to start copying from? So you pass it a pointer to the source memory location. Uh, you tell it where you'd like to copy the memory into. So you pass it a pointer to the destination memory location. And you tell it how many bytes would you like to copy. So that's this thing called count. Right. If you're copying arrays, you can copy an entire array into another array. You can copy part of an array into another array. Um, and you can copy parts of an array into parts of another array. Right? So uh they're very flexible functions, right? Their flexibility comes from uh the fact that they use pointers to specify where and uh from where you want to copy the memory from and where you'd like to copy the

1:34 memory to. So there's a fairly long uh example program here.

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.