Pointers and memory management

The things you likely never want to deal with but sometimes you have to

Two weeks ago I listed the mistakes most scientists and engineers make when programming. Here they are again:

  • No or insufficient version control

  • No automated testing

  • Huge functions with many nested control structures and large complexity

  • Uncareful memory management resulting in leaks and segfaults

  • Hacky patches upon hacky patches

  • Insufficient error handling

  • UI code inside functions that make calculations

I then went through the first three. I’ll be quicker with the rest. Today let’s talk about uncareful memory management.

The thing is, pointers and memory management is hardcore computer stuff. Scientists or engineers shouldn’t need to deal with it. They should better use a programming language that hides it.

The problem is, however, that scientific models often involve many calculations and consume much memory. So they need to be fast and memory efficient. Languages that manage memory automatically and don’t have pointers can be slow. R and Python are slow. These languages also often consume much more memory than they need to. Other languages such as Delphi are fast but sometimes in order to get the right speed you need to use pointers and manual memory management. I’m not certain how it goes with Fortran and Matlab.

Programmers also face these tradeoffs. When we use pointers to write fast and efficient code, we occasionally get some ugly bugs but at least we find that this kind of programming is fun.

In any case, if you do have to work with pointers and memory, you need to do it carefully. I’m not going into details about how; this would be too technical. However, if you do have this problem, I’d like to hear about it, so hit reply and tell me!