A web application is like some creature that is jostled, prodded and wishfully coerced into functional existence. Then one day it metamorphoses into some self-propelled gelatinous goo that slowly crawls out of a petri dish and into the tenuous world of production where one can only hope no unfortunate events occur. No wonder with all this talk about slime and protoplasm us developers never really want to get our hands dirty; in fact, I always try to code while wearing a biohazard suit. In a pinch I may resort to latex gloves, but only after being properly caffeinated and caught up on the latest reddit stories.
Every once in a while - it's human instinct after all - one needs to peek under the hood and see what's going on, no matter how squeamish you are. This is despite all of your training that such deep knowledge can be outright dangerous. Just click the "Next" button on the "auto code generator wizard" and be done. But you really want to know - has the goo changed colors? Will it cleanup with a wet spill kit? What makes this self-aware goo really tick? (How do you dispense the Turing Test to validate its self-aware state??)
And then it hits you. Somewhere in your code you have this array, say 1000 integers. Lots of other crazy stuff is going on, but then the near imponderable pops into your head: how much memory does this array use? Oddly I thought the same thing too (at the midnight hour nonetheless) and after to searching around online I found that an integer is commonly a 32-bit value which translates to 4 bytes (if you want the gory details it's 32 bits/8 bits per byte = 4 bytes).
Although most of what is on the Internet is true, I'm still a bit cautious and after responding to a compelling email from a gentleman in Nigeria I decided to write some code to test this theory:
#include <stdio.h>
int
main (int argc, char **argv)
{
int num = 1000;
int int_array[num];
int i;for (i = 0; i < num; i++)
{
int_array[i] = i;
}printf("Space used by int_array: %d\n", sizeof(int_array));
}
And here is a sample run:
staze-imac:~ staze$ gcc --version
i686-apple-darwin9-gcc-4.0.1 (GCC) 4.0.1 (Apple Inc. build 5465)
Copyright (C) 2005 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.staze-imac:~ staze$ ./memtest
Space used by int_array: 4000
Now isn't that something. Stand back - let's work out the arithmetic: 4000 bytes / 1000 integers = 4 bytes per integer.
I guess you learn something everyday. Perhaps more on some days as compared to others. That reminds me - I need to print out a bunch of Anti-Pattern Checklists for a design review meeting later today.
This is not true; sizeof(int) depends on architecture.
Posted by: jacekm | April 01, 2008 at 02:06 AM
... or it could be april fools?
Posted by: jacekm | April 01, 2008 at 02:08 AM
April Fool's indeed. Next year I'll try to incorporate time_t into the fun.
Posted by: Chris Staszak | April 01, 2008 at 08:55 PM