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
WEIRDEST Images of the Week: IMG! 11
You can buy pens at pen is.net. Wait! And the most awesome guitar ever! It’s episode 11 of [Music] IMG. Hey buds, sup player? Here’s Shaquille O’Neal, and here he is last weekend for Halloween as Shakita. And yes, he sang Beyoncé! But if you’re still not…
Visualizing Fourier expansion of square wave
So we started with a square wave that had a period of two pi. Then we said, “Hmm, can we represent it as an infinite series of weighted sines and cosines?” Working from that idea, we were actually able to find expressions for the coefficients for a sub 0…
'Pirate Birds’ of the Tropics 180 | National Geographic
This wildlife refuge is on a remote windy island between Nicaragua and Costa Rica. It’s dedicated to protecting seabirds, especially the large populations of brown pelicans and magnificent frigate birds that nest here. Frigate birds have extraordinarily …
Caesar, Cleopatra and the Ides of March | World History | Khan Academy
[Instructor] Where we left off in the last video, we saw Julius Caesar had conquered Gaul as proconsul. And, near the end of his term as proconsul, the senators in Rome were afraid of him. He was this popular, populist, charismatic figure; he had just had…
The reason why your life is so boring and how to change it
You wake up tired. You work for long hours. You come back home and rather doing something you truly enjoy, you either pick up your phone and turn on Netflix and simply waste your time. You already know hitting the gym, reading a book, or going for a simpl…
Sound + Fire = Rubens' Tube
So Dr. Phil, uh, what’s going on here? Okay, what we’ve got here is a metal pipe with a whole lot of holes in it. We’re pumping gas through it, and we’ve lit it up, as you can see. So we have like a whole lot of, uh, buns and burners all in a row—a whole…