Linux + C – Malloc and Free

In the last post, we employed dynamic memory allocation. Today, we're looking at that concept in further detail. Allocating Memory The function malloc() - memory allocate pulls a section of the heap - memory that exists outside of the usual program stack which is used to store data members - and returns it to you. The

Linux + C – Linked Lists are Applied Pointers

We now understand (at least a little bit) how we can work with pointers. We're now going to look at one of the simplest, but most useful, data storage structures - the Linked List Referencing a Pointer Before we go any further, we need to look at a bit of syntax. We know that when we

Linux + C – Arrays as Pointers

We've dealt with arrays a little bit in the past. Did you know that that whole time, we were dealing with pointers? Array as a Pointer When we declare an array, we are really doing the following things: Allocating a section of memory large enough to contain N objects of the chosen data type Creating a pointer to

Linux + C – Introducing Pointers

You've likely noticed the * symbol showing up in our code. Time to look at what that means. Value and Reference There are two kinds of data in a program: values, references to a value. We already know how to create a value object, because basic data types such as int and char are value objects.

Linux + C – Structures, Switches, and Your First Header

This will be a bit of a longer section, because I couldn't think of a fun way to write the code otherwise. Bonus! Structures A structure is a data element defined by the programmer which contains several member data elements. We define these structures to allow ourselves to handle a set of related data much more

Linux + C – Manipulating the Precompiler

Ever wondered what the whole #include thing really means? Of course you have. Precompiler There are basically three stages that gcc goes through when it compiles your program: precompilation, translation, and linking. At this point, we're interested in that first stage. The precompiler is the program which reads and organizes your C code for the translator to

Linux + C – Reading from stdin

We know how to send input from the terminal. Now we'll look at the right and wrong ways to do keyboard input within your program. Reading Input from Standard Input Have you ever seen a program which used a prompt to ask for something? For a simple case, it could look like this: Username: >> Password: >> There are numerous functions

Linux + C – Adding Arguments

It's finally time to look at those arguments we pass into main() - int argc and char *argv[]. Terminal Arguments You might have noticed that vi and gcc are both programs. You might also have noticed that we always call these programs with arguments. You might also notice that, up to this point, we haven't called any