
Helloooo! I am Moose! They/Them/He/Him I am a embedded software engineer with autism, depression and anxiaty ( Wooo! ). I post about... whatever I want... software things, mental health things... whatever I feel like Feel very wellcome to send me asks about... anything that strikes your fancy :3
266 posts
Calling By Reference In C++ Also Known As STOP OVERTHINKING EVERYTHING
Calling by reference in C++ Also known as STOP OVERTHINKING EVERYTHING
So I have now seen a specific type of horrible code from several programmers at my internship who really should know better. The code will look something like this:

And when I ask how on earth they managed to overcomplicate "calling a function" I get the answer "I am calling by reference!" Which... no... no you are not. So, there are two ways to feed arguments to a function. Known as calling by value, and calling by reference. When you call by value, the compiler takes a COPY of whatever you send in, and your function works on that copy. (By the way, that is faster than calling by reference when you need to send in small primitive variables, since playing with a 64bit address is harder than moving the 16 bits an int often is ) What is happening in the... abomination of a function call here, is that they use a shared pointer. And shared pointers are a great tool! It gives you all the safety and easy of use of working in a single thread environment with a statically allocated variable (IE, a variable allocated on the stack) when you are working with dynamically allocated memory in a multi-threaded environment. But when you use it on a statically allocated variable in a single threaded environment... then.... it gives you all the safety and easy of use... that you already had... When I asked why on earth they were doing this to that poor poor pointer, I got told that it was because using raw pointers was bad... Ok... first of all, yes, raw pointers are bad. Not really in of themselves, but because c++ have better tools than them for 99% of use cases... But that is not a reason to use a pointer at all! When I told him he was calling this function by value... just in a really weird complicated way, he disagreed. He was using a pointer! Yes... you are putting in a pointer... which gets copied... And together with the work on creating the pointer and the more complicated syntax of using a pointer... it is just bad. You know how you call by reference? You do it... by calling... with a reference... Not a pointer, a REFERENCE. The hint is in the name! This is the refactored code:

THAT is calling by reference. It is quick. It is clean. It is easy. The ONLY change from calling by value is in the signature. Both from the perspective of someone who uses the function and from inside the function, it works JUST like a call by value. Just like using a pointer, it allows you to work directly on a variable from an outher scope instead of copying the variable into the function. And unlike a pointer, a reference guarantees that whatever you are being feed is instantiated. You can send in a null pointer, you cannot send in a null reference (Well...Technically you can, but you have to work really hard to be able to.) Like... often in programming, the easiest way, that requires the least amount of work, is also often the most efficient and the easiest to maintain... just... just do it simply. I beg of you. Code models reality, and as a result can get really really complicated. So let us keep the simple things simple, yeah? ( By the way, I am grumping at programmers who should know better. When newbies makes weird coding choices, that is simply them learning. That is obviously fine )
-
moose-mousse liked this · 1 year ago
-
mbvisualarts reblogged this · 1 year ago
-
mbvisualarts liked this · 1 year ago
-
neospidey liked this · 1 year ago
-
neuroglitch liked this · 1 year ago
-
thelmanotvelma liked this · 1 year ago
-
paradox24universe liked this · 1 year ago
-
chqnnn liked this · 1 year ago
More Posts from Moose-mousse
A beginners guide to GIT: Part 4 - How to use GIT as 1 person
Table of content: Part 1: What is GIT? Why should I care?
Part 2: Definitions of terms and concepts

Part 3: How to learn GIT after (or instead of ) this guide.
Part 4: How to use GIT as 1 person

Part 5: How to use GIT as a group.

When it comes to not getting in each other's way, working alone is the simplest (It has a lot of other drawbacks). This is the simplest way to use GIT. You can do it with an external repository as a backup or just locally on your computer. It depends on how important your project is. If your laptop crashes tomorrow, which projects would you have a really hard time losing? Better to have an external backup for that. Github is often used for this (Maybe less now that Github makes machine learning AI’s, and so ARE stealing your code to train their AI on.) but you can also use Bitbucket (Which... may also steal your code...) and there are many many others out there. GIT is often used in certain patterns, called “workflows”. These have you working in more or less rigid ways to make it simple to work together. But since you are working alone, you do not risk others changing your code while you are working, so you can do it the simplest way :D
I will be doing a step by step guide that you can follow along. I will be doing it on a completely empty project and making a tiiiiiny program in C. This is because it is super simple. You do NOT have to know C to follow. You can also follow the steps with your own already existing project.
I PROMISE you, GIT cannot hurt you. Worst case scenario is that you fiddle around and break the repository part. (Meaning the files in the .git folder). But your files will always be safe.
(If you do not have git installed, check out part 3 for that)
First, I make a folder, navigate my shell into it, and call git init:

By the way, you can get used to GIT messages like this that tell you all your options, and explain what GIT has done for you. GIT is very good about giving you as much help and info as possible,
Now I will teach you the most important command in GIT.
It is more important than any other. Ready?
git status

This makes GIT tell you what git thinks is happening right now. What issues there are and what files are tracked, untracked or have been changed. Use this command often, especially while you are new to GIT, run it after every other command. It is how you learn what GIT is doing and thinking :3
Since our repo is empty it tells you what branch you are on (master. The only branch we will need since we are working alone)
and that you have not made any commits.
It also tells you the commands git think you will want to use on files. Since our repository is empty, it tells us to create some files, and then how to add them :3 So let's do that:
I have added my tiny program, as you can see:

Now let us see what GIT thinks we did:

Now, since there have been changes, git shows us them.
Files can be untracked tracked and not changed (In which case, git status does not show them) tracked and changed.
Right now, main.c is untracket. Which basically means GIT have no idea about this file, other than it is in the folder.
Ok, let us commit(save) the file. GIT tells us this is done with git add <File> . So we will write git add main.c
Then we use git status again to see what happened git status

And yeah, our file is now ready to be committed. So lets do it! git commit -m “My first commit!”
The “-m” option is to write the git update explanation directly in the console instead of using an external program to do it. Done You have now committed your code! It is now saved!
git status shows that everything in the working tree is as it was last time we committed (Duh. We JUST committed)

I will now make some changes to the main file:

Git status shows us main.c was changed...but what if we wanted to know what was changed in more detail? How will we get status to do that for us? Let us find out! git help status
git then shows the help page for status And there we can see this part:

So if we write status with 2 -v arguments, we get all the details. Let us try:

And look! It shows us EXACTLY what lines were changed! I stage the changes and commit:

And you have now learning enough about GIT to use it.. You now have all your work saved, in different commits. If you ever want to know all the commits you have made, write git log:

And if you want to know what a specific commit did, you copy the name of the commit, and write git show:

Now, everytime you want to save your work, you
1: Write/change the files you want
2: Add the files you want as part of this commit
3: make the commit These three steps are your workflow.
If you have a remote repository, then you add them steps
4: push to remote repository
To do this step, you can actually just write
git push
If you have set up a remote repository, then it just works. If you have not, then git will tell you what to do Whichever remote repository you use will tell you if you need to do other steps, like setting up passwords or ssh keys. They will also tell you how to set up the remote repository (That is not a GIT thing, that is a bitbucket or a github thing, so refer to whichever of those sites you want to use) And that is all! Every time you commit, your project is saved (it is smart to commit often, but usually only commit when your project can be compiled.) And whether you use a remote repository or not, you now have a fully valid GIT repository, and all the git tricks can be used on your project!
Help learning c++
So at my internship a whole bunch of people have to switch to C++. And because management is incompetent, there are no learning package or company resources to help this. So I am converting some of my code notes to help this along. And I am also sharing them here. They are created on, and meant to be used on godbolt. They contain explanations and some of them instructions for how to investigate what you are looking at. The ones I have for now are: Understanding objects ( Constructor, destructor, assignment and copy operators ) https://godbolt.org/z/EaMc1fW44 Access modifiers (IE, do you know what public, private and protected means?) https://godbolt.org/z/v3nejaYhh
Pointers (Basically, to not use raw pointers and what to use instead) https://godbolt.org/z/4745PK38n
A beginners guide to GIT: Part 1 - What is GIT? Why should I care?
Table of content: Part 1: What is GIT? Why should I care?
Part 2: Definitions of terms and concepts

Part 3: How to learn GIT after (or instead of ) this guide.
Part 4: How to use GIT as 1 person

Part 5: How to use GIT as a group.

GIT is a program used via a shell that allows you to save your text based work.
… no really, that is it. That is what “source control” basically means. Nothing mysterious, nothing complex. It saves. But it saves in a way that gives you extra features! So you can also:
Go back and forth between any time you saved in seconds (So the saves are more like saves in a video game, than saving a file)
Have GIT tell you what changes happened between 2 saves so you can find bugs
Have GIT find which commit last touched a function or file so you can find out when bugs got created Sexy right?
Furthermore, GIT allows many people to each work on the same project at the same time. Even the same files and the same code. There are other fringe ways to use GIT, but most use one of 2 ways: 1: Local repository. Basically, a local save, used just by you. Easy peasy. 2: Remote repository. The project has a central save file on a remote server. Everyone wanting to work on the project copies this, and starts working on their different parts. When someone has a chunk of work done, they update the central save file with their work. When you do this, GIT makes sure that the different changes do not interfere with each-other. And if they DO interfere, GIT makes it as simple as possible to merge the 2 changes together.
And it does it while using less than a dozen kilobytes of space. Even for huge projects.
Here is the second best thing. GIT scales perfectly from a single person project to gigantic projects with thousands of developers. You can use GIT , and only GIT , and no matter how your project grows, GIT will still be a good fit for you.
And the best thing? The complexity of using GIT, scales with the complexity of the development environment. For a single person, it is super simple, and completely pain free For small teams, it needs a small amount of work and agreements For large multilayer development teams, it needs somewhat rigid standards for doing things and several people dedicated to making it run smoothly.
This is why the “Use GIT” advice is so universal. Because no matter what you are doing, if it is text, then GIT is THE source control tool to use. When your code is breaking at 2am and you have lost track of what you changed when and where, GIT will save you! Many newbies (me included) who did not use GIT, have had to scrap entire projects because they simply could not un-break them again.
What are strings???
So, on the codeblr discord there was a question which is hard to answer unless you already fully understand what a string is... So, what is a string?
And this is a great question! Because to answer it we will learn a hell of a lot!
To answer this, one needs to know what an array really is. This is important in any language you write in. But in most languages it only becomes important to know when you get to optimizing code.
So for most, it is fine to think of arrays simply as collections. But in C, you need to know what they truly are to work with them (Which is why newbies are often taught C to learn these things)
I will use drawings and links to godbolt code examples I made, to help you understand things.
To explain what strings and arrays are, I will use a trick that you may also want to copy if you have a hard time learning concept in programming or electronics.
Try to to "re-invent" it. By going step by step into the things that required it to be invented, and how it was done.
So arrays. They are a way of storing many variables in one place, and work on them very very efficiently.
We start with having variables. Each variable have an address where they are in memory, and a value at that address.
An address is just a number. Nothing more. But to make it clear when we are talking about them, traditionally you write it in HEX. These numbers are marked with "0x", as in, to write "6" in Hex, we write "0x6"
Now, on each address you can store 8 bit. To store values that are larger, we use more than one memory address.
Ok, with all info, let us draw the example we will use, and write the code:
We will have 3 numbers. Each takes 16 bits:

https://gcc.godbolt.org/z/cse64Kd6x
(Notice that the adresses counts DOWN. This is for stack and heap reasons we will not explain here. But just know that we count DOWN, not up when it comes to adresses on the stack, which is all we will work with here.)
If we know the type of a variable, we also know how much space it takes.
So to really know a variable, we need to know its type, and its memory address.
Now, if we create these variables one after the other they will be placed right next to each other in memory.
Then we can create a pointer! A pointer have a type they point to, so it knows how much space the variables takes, and a memory address. We will create it pointing at the first variable!
Let us draw it on the example:

https://gcc.godbolt.org/z/9abqMjrs3
And now, if we want to work on all of our values, we simply do whatever we want, and then subtract 2 from the pointer to get to the next value, and then work on that one, and so on.
If we want to add 1 to every value, we could do this:
Step 1, add 1

Step 2: Move pointer

Step 3: Add 1

And so on and on! :D
https://gcc.godbolt.org/z/7KGadT868
This is how arrays works. But instead of us having to remember to count DOWN, and what size variables are we have gotten all this build into the language. It also GARANTEES that the variables are placed next to each-other in memory (This is not actually guaranteed when you use single variables like we have in the examples... NAUGTY US!)
So this code does exactly the same as we just did. And as you can see it is MUCH easier:
https://gcc.godbolt.org/z/KWxc939v4
So as you can see, a array is... more or less a pointer that you cannot change (because it always point at the first element), also known as a const pointer. And instead of moving the pointer, we use the index... which is just "The adress of the first variable, and then move it down as many times as the variables size times the variable number we want"
And a string... is a specific kind of array we often need, because we often need strings of characters. A string is a array containing elements that are characters.
If you are using ASCII, that is 8 bit signed values
If you are using UTF-16 then it is 16 bit signed values
See? It is super easy to explain what strings are!
... IF you know what arrays are... and why we use them....
I just downloaded a program, as a git repository. It allowed you to have it work in several different ways. Which was achieved by simply using "git checkout" to check out whatever branch you want. I love it. That is so smart!