Apr 11, 2009

pointers

Pointers are the one unique "data type" in C. Now that I'm slightly more acquainted with the language, I realized that C is all about pointers and memory management.

What makes pointers and memory management so useful is that they allow dynamic objects to exist during run-time. This dynamic behavior is absolutely essential because most of the time we don't know how much data we will get from user.

Take an input string, for example. This is trivial in higher level languages like Python, but a real difficulty in C. Normally, C has to know what length an array of characters (a so-called "string") has to be in order to input them from standard input, but if I can allocate memory dynamically, I can set this size to any size I want, and expand it if necessary.

Pointers, aside from being a useful tool for passing parameters by reference, are intimately related to memory management, because pointers is the way in which memory is addressed.

One might wonder, where are these features in other higher level languages? Most of them would be either hidden ("unrecommended") or non-existent. Why so? If pointers are that flexible, then why don't we have them in higher languages?

The hard part about pointers and memory management is that you have to know how to use them, and to use them correctly. Messing around with them lead to things like the so-called "memory leaks". I consider the name a bit misleading... It's not like memory is vanishing or anything, but rather, the program is "hogging" memory even though it no longer needs it.

That's the problem with manual memory management: it's useful, but it can be also dangerous. It's "dangerous" because it can literally cause unexpected side-effects to one's own program. It's not just a matter of "memory leaks": other and more dangerous issues include unintentionally editing the data where you shouldn't, or trying to read data from the wrong place.

Either case, if the program decides to overwrite its own data but at the wrong place, the error may go unnoticed. If it attempts to read or write to memory that doesn't belong to the program, the operating system will refuse and cause the so-called 'segfaults'.

"With power comes responsibility" describes the situation quite well here. The direct manipulation of memory allows C to perform tasks efficiently, if used well; on the other hand, if it's not used well, it can lead to all sorts of unexpected errors, which may be either obvious or, even worse, subtle.

2 comments:

Adino said...

Higher level programming languages probably handle character arrays behind the scenes. In any case, I cannot stand programming in C (probably because I'm not very skilled). I prefer higher programming languages because I don't like to 'reinvent the wheel' every time I try to program something.

Freiddie said...

"Reinventing the wheel" is a nice way to put it; it's definitely easier to do things in Python than in C.

Post a Comment