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
The TRUTH About Shark Tank REVEALED! (Behind The Scenes)| Shark Tank's Kevin O'Leary
It had never been done before. Nobody had ever done this before and we got it done. We’re all stuck in this call at Hotel California. We went in, we never came out. During the entire season that we shot [Music] [Music], it ain’t bragging if it’s true. I m…
50 Founders Share How They Got Their First Customers
Did you find your first customer surprisingly? “A cold email? That’s a good question. A lot of calls and emailing on Reddit, actually. We don’t have them yet; we are currently looking.” [Music] “The first one was actually just kind of very crafted cold…
The Craziest Philosopher of All Time
The abstract world of philosophy is interesting. From stoicism to nihilism to absurdism, there were many different schools of thought trying to teach us how to think, act, and tell right from wrong. But have you ever felt that philosophy is sometimes a bi…
6 Millionaire Habits That Changed My Life
What’s up you guys, it’s Graham here. So throughout these last few years, I’ve had the opportunity to speak with hundreds of people about their financial situation, learn what sets them apart, and see firsthand how they’re able to grow their wealth from n…
Newton's law of gravitation | Physics | Khan Academy
The mass of the Earth is about 6 * 10 ^ 24 kg. But you know what? I always wondered, how did we figure this out? How on Earth do you figure out the mass of a planet? Well, we did that by using Newton’s universal law of gravity, and in this video, we’re go…
Climate Change Efforts Must Be Practical and the Time is NOW
How do we develop climate-oriented investments that are both going to move the needle in a significant way and also generate a significant enough return for investors? Yes, well think about it. It’s your pension fund, and now because it’s your pension fu…