19 Aug

Out of Memory?

It’s a common practice not to check what malloc() or new returns, since in an ideal world you will never run out of memory and so allocating memory will never fail. With a reasonably sized page file that is mostly true. When using a page file, this wouldn’t be a problem since if there is free space on the drive the page file is located, virtual memory space can be temporarily enlarged (and a message pops up mentioning that) and the application that tried to allocate memory got what it asked.

However, since I upgraded to the maximum amount of memory Windows XP supports, and switched memory paging completely off, I’ve noticed some programs will silently fail when the system runs out of memory. And this isn’t that uncommon since I develop software and the crap programmer I am, silly mistakes are and always will be made that cause huge memory usage (and there’s also the rest of the software running on the computer). I have noticed at least Firefox simply disappears, while some software die with an error message.

For a game this wouldn’t be an issue (or a web browser even) but there are tons of programs downloading stuff or just hanging there that you’ll never notice missing. But it’s annoying to notice everything isn’t as you left it.

So, in case you haven’t thought of allocation failing, you probably should do something about it. When there’s no free memory left, even a malloc(sizeof(char)) will invariably fail. A lazy way out would be wrapping the allocation with something that checks if the allocation succeeded and if not, displaying a message box will halt the program until the user does something (kills the offending app reserving all that memory or frees some disk space for the page file) and then clicks “Retry” and then the wrapper simply tries to allocate again. In C++, the new operator can be overloaded.

Just my 2 cents (Euro).

Example code

C

void * my_alloc(size_t bytes)
{
  if (NULL == (ptr = malloc(bytes)))
  {
    // malloc failed, do something about it!
  }
  return ptr;
}

C++

This uses the function from above C code.

void * operator new(size_t bytes)
{
  return my_malloc(bytes);
}
Reblog this post [with Zemanta]
05 Aug

Introducing Calculon

Here’s a desktop calculator I wrote because I thought the standard Windows Calculator sucks (which it does). I got annoyed of how you can’t paste more than a single number and how the scientific mode misses a simple shortcut to square root.

Calculon

My approach is closer to the Google Search calculator feature in that the program only has two text fields, one for the expression and one for the result. It also does conversions and has user-definable functions, constants and whatnot.

Currently, the software is in alpha state (which means it works but is not finished) which I hope will not be perpetual. It is already very much usable, though.

What is left to do is that currently the software does only 80-bit floating point arithmetics (the best precision offered by x87) and displaying the result is double precision (thanks to the fact MinGW uses Windows libraries for printing floats, which have support only up to double precision). I also plan adding simple graphical features for graphing functions.

You can find Calculon (a placeholder name) on its own project page.

04 Aug

Is X Bullshit?

X… Score
Sounds too good to be true1 +1
Competes with an existing theory while offering simpler, more convenient (and often incomplete, less explanatory) explanations2 +1
Main proponents are willing to test the theory/claim for validity -3
Someone directly or indirectly benefits of people accepting it as a truth, as an individual (i.e. someone sells the more books/placebo the more people believe in it)34 +2
Existing knowledge undeniably makes it impossible5 +3
… and the main proponents ignore (and suggest followers to ignore) existing knowledge that clashes with their own ideas6 +4
Has peer-reviewed research and/or credible evidence in case it is completely new idea -2
Is a radically new idea7 +1
Main proponents and experts are from the same field (e.g. evolutionary biologists defending natural selection vs. mathematicians defending creationism)8 -2
People supporting the claim or theory are more interested of defending their position and/or creating more supporters rather than researching the subject9 +1

Links

  1. Cold fusion
  2. Intelligent design
  3. HeadOn, see this
  4. Immortality devices by Alex Chiu
  5. Homeopathy
  6. The Flat Earth Society
  7. A lot of sound science goes here, including quantum mechanics, relativity and such but they satisfy the peer-reviewed research requirement
  8. Creationism has tons of this, including using them as an authority rather than a guy who knows about things. And tons of Steves disagree.
  9. The whole climate change hassle fits here, albeit only because while the supporters probably are right, they use their energy to moan about the subject and boosting their egos instead of doing anything.