Every CS undergrad’s favourite topic
Once upon a time, you find yourself sitting in SFU’s Embedded Systems course trying to contain your unbridled excitement about your absolutely revolutionary idea to create a one-of-a-kind smart home system (sans spying, of course), while the honourable Dr Brian Fraser reads over the course syllabus in the distant, hazy fog that is your limited attention span. As you daydream, the whacking sound of seats returning themselves to their initial state wakes you and you see your peers filing out of the room. Class is FINALLY over! Time to race home to design and implement your (totally original) smart home system! You sit down to your XPS, boot up VS Code, and start up your Ubuntu image on your VM because let’s face it, you don’t have what it takes yet to fully commit to the Arch Linux elite.
Uh-oh! As you open a new file, you realize you forgot anything and everything about C programming –
malloc who? Panicking, you rush over to YouTube and 2x speed through a crash course on C in an attempt to scrape off the rust (this one is decent)…
Congrats! You are now finished restrengthening those neural pathways and are raring to go! You start to carve out the architecture of the project by implementing the header files for the modules you had previously designed. Once you finish those, you are now onto the nitty-gritty of it all, implementing the actual functionality. All is honky dory until you realize that you want to pass an “out buffer” to a write function you implemented, but you didn’t pay enough attention to the pointers topic in the video. Erg… pointers…
Ok, no sweat, let’s rekindle that love.
First of all, what is a pointer in C?
A C pointer references a piece of data. However, a pointer is not to be confused with being the data itself, it just references it. It does this by storing an address to the location in memory so that we may access the actually value of the data. In otherwords, a pointer IS an address that points to some data stored on your computer.
Differing ways to declare pointers:
int *somePointer; int* anotherPointer; int * yetAnotherPointer; int* meow, bark;
Now that we have declared some pointers, let’s do something with them!
Let’s try simply assigning a pointer some value:
int *somePointer; int myInt = 5; somePointer = &myInt;
- line 1: declares a pointer
- line 2: creates an
myIntand set its value to 5
- line 3: we dereference
&which grabs the address, and we set the address of
malloc? Now let’s try allocating some memory, heap style:
int *anotherPointer; anotherPointer = (int *) malloc(sizeof(int)*3); anotherPointer = 13; anotherPointer = 42; anotherPointer = 99;
- line 1: declares a pointer
- line 2: uses
mallocto allocate memory of the size int * 3 to your pointer.
- This pointer now points to an array that has been allocated enough memory to store 3 integers.
- line 3: sets the data at the first offset in the array to 13
- line 4: sets the data at the second offset in the array to 42
- line 5: sets the data at the third and final offset in the array to 99
Finally, let’s clean up our allocated pointer so we don’t cause any memory leaks:
Hopefully, now that you have had a refresher on pointers, you have fallen back in love with them also and are ready to make $$$ creating your revolutionary smart home system! Whew, the end.