Sunday, June 15, 2008

Make the portions smaller

Nowadays we are seeing article after article about rising food prices. Restaurants are getting larger bills from their vendors for all sorts of basic staples. Employees are demanding higher wages to pay for their own groceries at home. At the same time our waistlines are expanding and quite frankly we Americans are all eating just a little too much (myself included). So here is the solution to the problem : Reduce the portions in restaurants. Keep the price on the menu the same, just give me less food. That's right, give me less for my money. I'll still enjoy going out to eat. I'll still spend the same amount of money. I'll just look better doing it. :)

Monday, October 15, 2007

Not really "owning" your house

I am in no way qualified to give people financial advice, but it seems to me that many people are simply buying too much house. They have fallen into this trap of their home as their greatest "investment". They end up taking out a mortgage to buy the house. Then they take out a home equity loan to finance day to day expenses because they don't have an adequate cash reserve and are spending too much money on their house. Later in life, they have not saved enough for retirement so they end up with a reverse mortgage. Did they really "own" a house? Was a house really a great investment?

Friday, October 5, 2007

Free'ing memory in C (free_ptr)

What?
The free_ptr(void** pptr) function causes the allocated memory referenced by *pptr to be made available for future allocations and sets *pptr to NULL.



void free_ptr(void** pptr)
{
if ((void **) 0 != pptr && (void *) 0 != *pptr)
{
free(*pptr);
*pptr = (void *) 0;
}
}



Why?
In general, to avoid bad code that would normally get written after the call to free(ptr). This bad code may not be written until weeks or months later and it may not be written by you, but it will get written by someone. :)

  1. Double Free - After you free the memory referenced by ptr bad code comes along and does it again. Results are unpredictable and system dependent.


  2. Dirty Read - If ptr is not NULL after it is free'd bad code could be added that tries to read the contents of the memory at ptr. Worst case is that 99% of the time the memory will contain the same values as it did before the call to free(ptr), but 1% of the time it will not and you will end up with a bug that is hard to reproduce. If you try to read from NULL, on the other hand, you will likely get a segment violation, garbage data, or some other signal from the operating system that you have done something wrong.


  3. Dirty Write - Bad code gets added after the call to free(ptr) that tries to write to ptr. Sometimes nothing happens because no one else is using the memory and sometimes everything blows up.