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
A Warning For The 2023 Stock Market
What’s up, Graham? It’s guys here! So, 2023 is already off to an interesting start because, in just the last week alone, we’ve seen a woman go viral for buying a 1998 Ford Escort for 289 dollars a month for the next 84 months. A teacher was charged for ru…
2015 AP Chemistry free response 4 | Chemistry | Khan Academy
Answer the following questions about the solubility of calcium hydroxide, and they give us the solubility product. Write a balanced chemical equation for the dissolution of solid calcium hydroxide in pure water. So, we’re going to start off with calcium…
The city of walls: Constantinople - Lars Brownworth
Translator: Tom Carter Reviewer: Bedirhan Cinar The most important walls in western history aren’t even in the West. They surround the modern city of Istanbul, Constantinople as the Romans called it. And for a thousand years, the fate of Europe depended …
The 100,000-student classroom - Peter Norvig
Everyone is both a learner and a teacher. This is me being inspired by my first tutor, my mom. And this is me teaching introduction to artificial intelligence to 200 students at Stanford University. Now, the students and I enjoyed the class, but it occur…
Where No Grid Has Gone Before | Breakthrough
We don’t go to them and say, hey, we’ve got electricity. We’re going to bring it to you. We’re going to bring you modern entertainment that electricity provides, no. They’re coming to us and saying, we’re so far off the grid, we don’t have any electricity…
Sam Harris: The Self is an Illusion | Big Think
What one of the problems we have in discussing consciousness scientifically is that consciousness is irreducibly subjective. This is a point that many philosophers have made – Thomas Nagel, John Sorrell, David Chalmers. While I don’t agree with everything…