yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Mac Programming Lesson 2 part 2


4m read
·Nov 3, 2024

Okay, so here we go.

And as you can see, food temp is going to be originally where foo is pointing, and food is going to be originally where bar is pointing. Bar isn't going to be pointing anywhere, which is kind of really useless to have bar even sitting around. But bar isn't a waste of memory in any way, so what we can do is we can do bar equals ns string alloc init with string. Now I'll assign it to be "howdy."

So now we're going to have three strings allocated in memory, presumably right next to each other, that we're going to be able to be accessing through foo temp, foo, and bar. So now, here we go: three addresses right there. So this is how pointers work. That's why you use a star. All that variable actually is, is like a four-digit little number telling the computer where you actually want to go. And that's also what a memory leak is when you have no variables anymore pointing towards where that variable originally was.

So hopefully, this was eye-opening to you in some way. If it wasn't, please comment or personal message me and ask me some questions. I'll be open.

Um, so now we're going to go over something called malloc. Malloc is a C thing you should have done, but I never got around to it. Malloc basically allocates stuff. So it alloc. But you can malloc a certain amount of space. So I can malloc, say, 20 places in memory — 20 pointers to be this for 20 bytes to be this one pointer. So you may know about care star. I can assign a care star, and the care star is characters. It's a pointer to a character.

So let's say I want to have foo be 10 characters. What you may have done originally is care star left bracket 512 right bracket. All that really does under the hood is change this; it adds a star there and then it calls malloc 512. Later on, what I can say is foo equals malloc 10. And what this will do is this literally allocates 10 bytes and assigns the address to the first of those 10 bytes to foo. So now, foo will be pointing to this place in memory that is going to be used only for foo. That's going to be 10 bytes.

So now, if I do print, or no, sorry, nslog—there we go—we're going to see an address. So that's where foo is. It is in fact 10 characters. Let's say we malloc it to be one character. So now foo is just going to be one character. See, the address is exactly the same because that's only the starting address.

So here's how it works, basically. Foo is going to malloc. Returns a pointer in memory, so a pointer in memory such as 0x whatever that big number was. So does ns string alloc. That also returns a pointer. Now, pointers in Objective C are also represented as ids. Id just means, under the hood, a pointer.

So, like for instance, now, say we do foo equals malloc one. Again, we still have this place in memory that is one byte that is malloc. That means we own this one byte. The thing is that now we're making foo point to another byte. We still own this last byte that was one byte, but we can no longer have control over this byte. So that's a memory leak.

So the way we get rid of this old space in memory before we assign it to a new space in memory, we do free(left parenthesis and the name of the pointer right parenthesis). And that will go to where the pointer is pointing, deallocate everything there, so you no longer own it. It is free to anyone who wants to grab that piece of memory.

And then assign foo to be 0x0, which means nil basically. And now I'll malloc foo again, and after I print the pointer, I'm going to do free foo again. So this is now a memory free or a memory leak free application.

So, as you can see, let's say I don't free foo right here before we got this address the first time right here on this first malloc. Then we freed it, so that address no longer pointed to anything. Then we malloced it again; it looked for the first possible address it could get, and it was the same one it got before with this malloc.

And then it just used that until another free. If we don't free it, this address right here will continuously be taken up. So how will we follow the show? As you can see, it has to go out and grab a whole new address if we don't call free right here because this address is still being taken up by this malloc even when we reassign foo, which is why, in Objective C, you're going to be seeing a lot of release in dialogue stuff.

And that essentially calls free, and alloc essentially calls malloc. So this is how to manage memory. This is a huge major point to know. And if you do not know this after watching this video—which is understandable; I didn't get this for a year—then I beg you to comment on this video and tell me that you don't get what I'm doing because I will help you, and I will make sure that you understand.

Because once you understand this, you'll be a step ahead of everyone who doesn't. Most computer consultants these days who work at big companies, they don't know this stuff. So it's a good idea to be a step ahead. Now, you might not need to know this stuff if you're going to be doing things like Java, but if you're planning on doing Objective C and you're planning on actually knowing Objective C, then you better know this stuff because it's highly important to actually get what you're doing.

So this is memory management. Just keep in mind that a care star or an ns string star is really four bytes in memory pointing to some other place in memory that is the actual ns string. So this is hard to learn, and I get it if you don't get it. So once again, I'm really being repetitive here because I want to make myself clear.

Ask me any question, no matter how dumb it seems, because I'll help you. Okay, so anyway, this is Mac programming lesson two. Um, so hope you learned something. If you didn't, I want you to learn something, so ask me.

So thanks for watching, mac heads in one. Subscribe and goodbye!

More Articles

View All
❄️🇬🇧 London Snow Day 🇬🇧❄️
Wow, it finally snowed again in London! A snow day not to be squandered inside. I’m supposed to be working today, but does daily vlogging count? I’m not a daily vlogger, but I think if I make a vlog, that can totally count. Come join me as I do nothing m…
How To Get Rich According To Richard Branson
There are a million ways to make a million dollars, and in this video, we’re looking at how the rebel billionaire himself, Richard Branson, did it. When he left high school, his headmaster told him, “You’re either going to become a billionaire or end up i…
Dividing rational expressions | Precalculus | Khan Academy
The goal of this video is to take this big hairy expression where we are essentially dividing rational expressions and see if we can essentially do the division and then write it in reduced terms. So if you are so inspired, I encourage you to pause the vi…
The Warning Of Hyper Inflation | DO THIS NOW
So, as most of you know, I usually intro my videos with “What’s up, you guys? It’s Graham here.” But the only thing up today is inflation, and it’s getting much, much worse than most of us initially expected. Throughout the last week, it was revealed that…
15 Rules To Win At Life (Part 2)
This is the Sunday motivational video. Every Sunday, we bring you a different type of video which should improve your life. Today, we’re looking at 15 rules to win at life part 2. Welcome to a Lux.com, the place where future billionaires come to get insp…
What is the better deal? | Budgeting and saving | Financial Literacy | Khan Academy
In this video, we’re going to play a game that I like to call “What is the Better Deal?” So, let’s look at an example. Let’s say there’s a 16-ounce bottle of shampoo that costs four dollars. And let’s say there’s another bottle of that shampoo on the rig…