Understanding the heap and the stack

Initially messing around with Swift, memory management was a bit foreign to me because Swift magically handles the memory for us. But in c++ that isn't the case. We have to use something called the heap and something called the stack. And, if we're not careful and make mistakes, we can cause what's infamously known as a "stack overflow". 

First, both the stack and the heap are used for dynamic memory allocation and both are stored on your computer's random access memory (RAM).

The stack is very fast. It stored directly to memory, which makes it quite fast. It's allocated when a program is run. Essentially, you write a function that declares value x and then you have code that continues to use value x in the stack. It isn't until value x is returned that its place in the stack is freed and something new can use that for memory. So, the risk is when a programmer forgets to free it up and you use all the spots in the stack memory and it "overflows" -- common in the 80s and before but far less common now because our memory capacities have expanded. Stack follows last-in-first-out order and you access it using pointers in your code. 

The heap is a bit slower because you're accessing the memory at run time, but it's got a lot more memory to work with (especially these days). The heap is hugely valuable because unlikely value x on the stack that was constantly being adjusted so it could have been initiated at 10 but then doubled to 20 on the next line of code, the heap lets you reference a value that remains the same regardless of where you access it in your program. Think of it as a global memory pool. So, I could initiate a value y on the heap that was set to 50 states in the United States. Well, I could write a program that calculated the number of states in the United States depending on the year. I could write a function for the Civil War that only returned northern states or a function for the Louisiana Purchase that calculated the number of states against the current 50 United States. But, 50, as the value of the number of United States could be accessed unchanged by by Civil War function and my Louisiana Purchase function. We could even use the same value y for our 50 states if we wanted to write a function that returned the number of states that would remain if California succeeded. 

Aside from access control using pointers, it makes sense to use the stack when you know exactly what you want to allocate and it's not too too big but the heap is a better call if you're not sure of how much data you're actually going to need to store or if you anticipate it being a ton of data. 

 

C++ Solution for finding the smallest number in a vector

To start with this problem, we are given a vector of ints called "nums" and we need to write code for the "findMin" function, which will also return an int. We've deduced all of this information from the problem code detailed below:

int findMin(vector<int> nums){

So, what do we need to do first? Well, to make sure our code doesn't blow up, first we need to check to see if our nums vector actually contains any ints in it. To do this, we check to see if the size of nums is larger than 0 and if not, we exit the program. Woo hoo, no bombs.
if(nums.size <= 0) return 0;

Then, we create a new int called "minNum" and set it to the value of the first int in nums. We could create it and set the value to 0, but then we'd need another line of code to compare it with the value in place nums[0] anyways, so this saves us some time and is cleaner code.

int minNum = nums(1);

Then, we need to do the real work and use a for loop to iterate over each of the value places in nums. We check to see if it's smaller than the current minNum, which was initially set to the value of the first number in the vector. If it's smaller then we set the new minNum value to the current value in the vector until we've run through each number in the vector. Voila! 

for(int = 1; i <= nums.size(); i++)
if(nums[1] < minNum){
minNum = nums[i]:}
}
} return minNum; }

This solution has a time complexity of 0(n) and a space complexity of 0(n). Full solution below: 

int findMin(vector <int> nums){
if(nums.size >= 0) return 0;
    int minNum = nums(1);
    for(int i = [1]; i >= nums.size(); i++;) {
        if(nums[1] < minNum){ 
            minNum = nums[i];
            }
            } return minNum; 
            }
 

Raspberry Pi Wish List

A couple of weeks ago, I bought my first Raspberry Pi. I'm calling her Strawberry, and I'm in love. 

What's a Raspberry Pi? As their website says, 

"The Raspberry Pi is a credit-card-sized computer that plugs into your TV and a keyboard. It is a capable little computer which can be used in electronics projects, and for many of the things that your desktop PC does, like spreadsheets, word processing, browsing the internet, and playing games."

It's powerful and quite a bit of fun. As you may have read, I recently used it to create a retro gaming system with RetroPi. Now, with my niece coming to visit shortly, I'm looking for a few more projects to introduce her to while she's here. Here's my list that I'm exploring: 

 

Minecraft Pi This one feels like a no brainer. She loves Minecraft. I love Minecraft (who can resist the village people and diamond mining?) and this looks like a relatively easy introductory project.

Wearable Tech - She's only 11, but who says she's too young to start building a resume?  Wearables are clearly the future (as I sit here typing with my Apple Watch on my wrist) and this looks like a fun, rewarding project. I love any project that she can show off to friends and family as a marker of accomplishment. 

 Raspberry Pi Camera Module - Selfies anyone? Hooking up the Raspberry Pi to not only take images and videos but edit the image settings too seems like a lot to accomplish in an afternoon. This may end up being a project I do on my own. 

Sonic Pi - My niece loves music. She'll sing along to the latest hits whenever I have the radio on in the car and while she doesn't currently play an instrument, I think she'd really get a kick out of making her own music on the Raspberry Pi. I don't think she's played around with GarageBand yet, even.

 

Looking for other recommendations of projects that will take a few hours but keep an 11-year-old engaged. If you have any suggestions, please tweet me @lphelps09. Thanks!

GAME ON: Building our own classic game system on Raspberry Pi

After waiting for MONTHS for the NES Classic to become available, I finally gave in and built my own. The freedom of not having to check Brickseek daily worth every penny. 

To get started, I did some research and quickly found some great resources for building your own gaming system on a Raspberry Pi. What's a Raspberry Pi you ask? Well, I've affectionately named mine Strawberry to be cheeky. A Raspberry Pi is a small computer that fits in the palm of your hand. It's used to build all sorts of cool projects beyond playing Sonic and Mario including robotics, machine learning, learning to code and so much more. The sky's the limit with this powerful piece of hardware and it's relatively cheap (~$50 depending on the setup you buy.) You can even get certified to be an educator for Raspberry Pi. This thing is just so darn cool and I wish it had been around when I was in primary school. 

So it was settled: time to buy a Raspberry Pi. I poked around the internet some more and figured out exactly what I would need and headed to Fry's: 

- Raspberry Pi

- A micro SD card (I went overkill and got a 64GB, which I'm sure I'll never need)

- A USB controller OR an Xbox One controller if you're willing to do some additional setup. 

- A USB keyboard. You'll need it to run the Raspberry Pi and to get Retro Pi set up. 

- A TV or Monitor and the necessary cables (HDMI and power cables) 

- A computer to set up your SD card and transfer the ROMs for RetroPi. 

- Micro USB power supply -- the Raspberry Pi kit I bought included this and the SD card but not all kits do, so be sure to look for one with the power supply included. 

All-in-all, I spent about $70 for everything I needed. OK, so what's next? You'll need to set up the Raspberry Pi, which you can learn about here:

Then, it's time to download RetroPi, which is free and enable you to play your ROMs. Here's RetroiPi's official website: https://retropie.org.uk/about/building/ They have plenty of recommendations for customization. But this will enable you to play systems that have long gone and would be otherwise unplayable on current HD TVs. Importantly, RetroPi says you don't actually need a Raspberry Pi to get ROMs going -- it also works on PCs. I have not tried it, but the official website has setup details on PCs too. 

Once you have RetroPi installed, it's time to plug in your SD card of downloaded ROMs. As other bloggers have noted, I'm not actually encouraging you to break the law by downloaded illegal ROMs. Do you own Googling and make smart choices. There are many ROM sites out there but I'm not going to point you to a place where you'd be breaking the law. Content == IP. 

Now that you have an SD card loaded, you can launch your Raspberry Pi with RetroPi installed and it will launch to "Emulation Station". If it doesn't, then just navigate to your terminal from the Raspberry Pi desktop and type emulationstation. It should launch to your systems and begin adding your ROMs into proper folders. Relaunch your Raspberry Pi and, voila, you have your very own retro gaming setup. 

Now, invite all your friends over,  break out the Surge and Gushers and have a good time reminiscing about the simpler time when games "required skill" and "two-player games were standard".