After years of python hacking and all kind of projects (like netgrafio or smalisca) I wanted to do more low-level stuff and refresh my C/C++ skills. I’ll try to describe how it feels like coming back to C/C++ after 2 years of coding abstinence.
It’s low.. Very low
Having started a new project I didn’t feel like I’ve forgotten everything. It just felt strange to
declare functions and use header files. After initial difficulties I was able to run my code and compile it using -Wall
without any warnings. Perfect.
The project itself is about implementing SSL/TLS in C since I’ve never done that before. The executable will take an URL and optionally a proxy as arguments:
|
|
The very first problem I’ve encountered was parsing. While in Python you usually have pretty good string operations you can apply on your string, in C you have to take of everything. In my case I had to parse the proxy argument for its specific parts:
- schema
- username
- password
- domain name
- port
And if that wasn’t enought, I also have to take care about error handling in case the proxy URL is malformed. Speaking of error handling: It was indeed tempted to think of some try .. catch
construct. Naaah! I remember … The typical construct you should use:
|
|
Ok. What about logging? I remember having implemented some macros in the past and finally found this:
|
|
Great! At that point if was able to pass arguments to my executable and do some string parsing.
Pointers .. and more pointers
I do know some pointers arithmetics. But coming back from objects to pointers was a big mindset change. Almost everything is a pointer to some data structure, to some memory region etc. Using pointers the right way has always been a challenge. Let’s first have a look at some struct I’m using to collect information and store global accessible data:
|
|
So we’ve got pointers everywhere. I use this struct to collect the CLI arguments using getopt
. Besides that
I’m using globalargs
to store information which should be available in all “modules” (I don’t have any yet but
just for the explanation …). But let me explain this using some example:
|
|
When compiled and executed this produces following output:
|
|
As you might have noticed change_usernam1
doesn’t really change the username
in the myStruct
structure.
Using pointers to pointers I was able to make my desired change. What did I learn from this particular case?
I should definitely improve my pointer arithmetics :)
Memory management
For almost every data structure memory has to be allocated. While this in OOP (object oriented programming) is usually done
by just creating a new instance of a new object, in C everything has to be done “manually”. In our example allocating memory
for myStruct
should be easy and straight forward:
|
|
And don’t forget to free
your memory! That’s it for now. Now happy (C) coding!