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
President Obama Credits Mom and Hawaii For His Love of Nature | National Geographic
It’s something to see, is it not? Amazing! It’s great to meet. Wonderful. Thank you for… We just diving in? Are we good? I think so. Okay, come on. Yeah, so I understand that you’re a big fan of your White House science fairs and that you seem to enjoy a…
Overcoming Self-Hatred
Self-hatred is something I’ve struggled with a lot in the past, so this video is quite personal. The experience of self-hatred often goes together with depression and is basically a mechanism to cope with beliefs about oneself and our position in the grea…
Mars 101 | National Geographic
[Music] To the ancient Romans, the planet Mars was symbolic of blood and war. But to many people today, the red planet may hold the key for a bright new future for humanity. [Music] The story of Mars began about 4.5 billion years ago when gas and dust swi…
Nihilism: The Belief in Nothing
We all know how it goes. One day we’re born, one day we die. Everything that happens in between we know and understand, but everything that happened before and will happen after, we know nothing about. As a result, it’s really difficult to say what exactl…
Science Is an Error-Correcting Mechanism
So getting back to good explanations, where do these explanations come from? There’s currently an obsession with induction. Induction being the idea that you can predict the future from the past. You can say, “I saw one, then two, then three, then four, …
Investments and retirement unit overview | Teacher Resources | Financial Literacy | Khan Academy
Hello teachers! Welcome to the unit on investments and retirement. As always, I encourage you to go through the unit yourself. If you have limited time, at least go through the exercises and the unit test to refresh both your own understanding of this mat…