Wednesday, June 29, 2022

Stack overflow on Linux

...exceeding the stack limit is usually considered a segmentation violation, and systems with enough memory management to detect it will send a SIGSEGV [segmentation fault] when it happens.

A typical symptom in a C++ program running on Linux is getting a segmentation fault when entering an innocent function like pow(). To debug, decrease stack usage (e.g. if there is a static array, decrease its size) in the code before the segfault, run your program in debug mode, see if your program continued further than before. Unfortunately, the same program might be working on Windows without problems.

To increase stack size on Linux, use ulimit -s <size_KB>

Thursday, June 23, 2022

Benefits of wiki style documentation

Wiki style (no signatures/approvals) documentation of complex software projects (lines of code > 10K) has the following benefits:

  1. Wiki approach saves you from wasting time in publication and approval process. It enables quick updates which increases quality of content.
  2. You as the developer will be able to remember important details of design, especially if long time has passed since you last worked on it.
  3. You can hand-off the project to junior developers without wasting your time.
  4. You can easily extract a user manual from existing content and users won't bother you with questions.
  5. Code reviewers get a better idea of overall design which increases the quality of review comments.

Wednesday, June 22, 2022

Java: Table with numeric input and length checks

Sample code showing how to do numeric input and character length checks with a JTable:


Tuesday, June 21, 2022

Stop build when "C4013 ... undefined; assuming extern returning int"

Recently I had to deal with a bug in a C project whose root cause was fabs not functioning properly due to missing #include <math>. Finding the root cause involved diving into more than 4 layers of abstractions, it was not fun (!) When run from Visual Studio as a dll project, the code resulted in fabs(10) >= fabs(20) to be true! When I compiled the project with Visual Studio, it gave "warning C4013: 'fabs' undefined; assuming extern returning int". I would normally expect the build to fail in the linking stage. Probably the linker is able to find the fabs but since it was first assumed to be returning int (4 bytes) and fabs returns double (8 bytes), this will result in 4 bytes of the return value being ignored, which can cause funny values. For more about the reason why this behavior is allowed, see my similar trouble with malloc before.

Another interesting fact is that the code was working properly when run from a Simulink s-function. I assume Simulink includes math.h somewhere in its hierarchy. Here is the simplified code:

To prevent Visual Studio from building when 4013 warning exists, go to project Properties - C/C++ - Advanced - Treat Specific Warnings as Errors - Add 4013:



Wednesday, June 15, 2022

String input to Matlab C Mex file

When you call a Matlab C Mex function with a string input, make sure to use single quotes, e.g. 'hello'. If you use double quotes, e.g. "hello", Matlab will crash and burn!