Patterns: Initialize Everything

malloc(): memory corruption When you look at an error message like that, what could possibly lead you to believe that, a hundred lines up, you didn't properly initialize a size variable? After all, all we know is that this malloc() operation could not complete because the memory it should be able to touch is corrupted. Yes,

Pointer Arithmetic Matters

What's the first thing you think when I say the word: Pointer. Generally, you either think of a particular register (unlikely) or a hexadecimal number representing a location in memory (likely). Personally, I'm in the latter category, which is why this problem arose. I was in the middle of programming, and I decided to allocate

Patterns: Shielding Inputs with const

One of the key worries I have heard from those ill-informed OOP programmers is that C cannot protect inputs you pass into functions. They use private fields and retrieval functions to ensure that the stored value is protected from unwanted modification. However, this concern is addressed by C's const keyword. Taking a const value There are times

Patterns: Creation and Destruction Stack

Virtually 100% of all memory leaks are preventable, assuming the programmer knows where to look. In many cases, the leaks occur because a programmer has failed to destroy what he has created, but it can become more complicated than that. Creation Stack: The Order Matters In many cases, we allocate memory as we use it, and

Linux + C – Some other Facts about Pointers

Pointer Math Pointers are numbers, which means we can use math to change where they point. For example, in an array we store the pointer for index 0, then add to it in order to find the later indexes. For example, if we store a uint32_t (32-bit integer) array at the address location 0x8FFFFF00, the

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.