how-stuff-works

How to instantiate a Java array given an array type at runtime?

In the Java collections framework, the Collection interface declares the following method: T[] toArray(T[] a) "Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. If the collection fits in the specified array, it is returned therein. Otherwise, a ne...

What's the algorithm behind sleep() ?

Now there's something I always wondered: how is sleep() implemented ? If it is all about using an API from the OS, then how is the API made ? Does it all boil down to using special machine-code on the CPU ? Does that CPU need a special co-processor or other gizmo without which you can't have sleep() ? The best known incarnation of sle...

Why is "null" present in C# and java?

We noticed that lots of bugs in our software developed in C# (or java) cause a NullReferenceException. Is there a reason why "null" has even been included in the language? After all, if there were no "null", I would have no bug, right? In other words, what feature in the language couldn't work without null? ...

How does delete[] "know" the size of the operand array?

Foo* set = new Foo[100]; // ... delete [] set;You don't pass the array's boundaries to delete[]. But where is that information stored? Is it standardised? ...

Is there any way to determine the size of a C++ array programmatically? And if not, why?

This question was inspired by a similar question: How does delete[] “know” the size of the operand array? My question is a little different: Is there any way to determine the size of a C++ array programmatically? And if not, why? Every function I've seen that takes an array also requires an integer parameter to give it the size. But...

Why an abstract class implementing an interface can miss the declaration/implementation of one of the interface's methods?

A curious thing happens in Java when you use an abstract class to implement an interface: some of the interface's methods can be completely missing (i.e. neither an abstract declaration or an actual implementation is present), but the compiler does not complain. For example, given the interface: public interface IAnything { void m1()...

How many random elements before MD5 produces collisions?

I've got an image library on Amazon S3. For each image, I md5 the source URL on my server plus a timestamp to get a unique filename. Since S3 can't have subdirectories, I need to store all of these images in a single flat folder. Do I need to worry about collisions in the MD5 hash value that gets produced? Bonus: How many files could I...

Is the Microsoft.VisualBasic namespace "true .NET" code?

My dev team is getting ready to start a new project. The shop has been a "VB shop" since the days of VB3, but the prevailing opinion now is that we're a ".NET shop" and since C# was created specifically for .NET, whereas VB.NET was a retrofit, we've decided to move forward writing C# only. The controversy revolves around the question of ...

C for loop implemented differently than other languages?

I read the following in a review of Knuth's "The Art of Computer Programming": "The very 'practicality' means that the would-be CS major has to learn Kernighan's mistakes in designing C, notably the infamous fact that a for loop evaluates the for condition repeatedly, which duplicates while and fails to match the behavior of most other ...

Size of a byte in memory - Java

I have heard mixed opinions over the amount of memory that a byte takes up in a java program. I am aware you can store no more than +127 in a java byte, and the documentation says that a byte is only 8 bits but here I am told that it actually takes up the same amount of memory as an int, and therefore is just a Type that helps in code c...

Why is SomeClass<? super T> not equivalent to SomeClass<T> in Java generic types?

I noticed the specificaition for Collections.sort: public static <T> void sort(List<T> list, Comparator<? super T> c) Why is the "? super" necessary here? If ClassB extends ClassA, then wouldn't we have a guarantee that a Comparator<ClassA> would be able to compare two ClassB objects anyway, without the "? super" part? In other word...

How much memory does a thread consume when first created?

I understand that creating too many threads in an application isn't being what you might call a "good neighbour" to other running processes, since cpu and memory resources are consumed even if these threads are in an efficient sleeping state. What I'm interested in is this: How much memory (win32 platform) is being consumed by a sleepin...

Can every float be expressed exactly as a double?

Can every possible value of a float variable can be represented exactly in a double variable? In other words, for all possible values X will the following be successful: float f1 = X; double d = f1; float f2 = (float)d; if(f1 == f2) System.out.println("Success!"); else System.out.println("Failure!"); My suspicion is that there i...

Why is this code with several "or" statements slightly faster than using a lookup table in Java?

While looking at a micro-optimization question that I asked yesterday (here), I found something strange: an or statement in Java is running slightly faster than looking up a boolean value in an array of booleans. In my tests, running the below algorithms on long values from 0 to 1 billion, alg1 is about 2% faster. (I have altered the ...

Why does Java's hashCode() in String use 31 as a multiplier?

In Java, the hash code for a String object is computed as s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] using int arithmetic, where s[i] is the ith character of the string, n is the length of the string, and ^ indicates exponentiation. Why is 31 used as a multiplier? I understand that the multiplier should be a relatively large prime ...

How do code profilers work?

While I was working on an University project, I used a project-internal profiler made by an elder student, it was very basic but good enough since its task was to subtract times between two points of the code and to give statistics. Now, how does a professional profiler work? Does it preprocess the code to insert checkpoints or stuff l...

how does silverlight deep zoom work behind the scenes

hey guys i have done few sample projects (just for fun) using silverlight deep zoom. its really cool and exciting stuff. but i was curious to know about what goes on behind the scenes to achieve this sort of thing. the deep zoom composer generates many images and few xml files within sub folders of its data source. can anyone explain ho...

Process Explorer - How does the dragabble crosshair work?

There is a feature in sysinternal's process explorer that allows a crosshair to be dragged from the application to a control in any other application you are running and highlights said control. Does anyone know how this was achieved or if there is a .NET/C++ library out there that can be reused? ...

How do GUI builders work?

I'm curious, how does a GUI builder/designer work? My guess ( for Java ), is that it actually creates a JFrame and overrides the events in some way. However, this is only a guess. Can you offer some insight? ...

How malloc() and free() work

Hi, I am curious to know about the in details about how the malloc and free works. int main() { unsigned char *p = (unsigned char*)malloc(4*sizeof(unsigned char)); memset(p,0,4); strcpy((char*)p,"abcdabcd"); // **deliberately storing 8bytes** cout << p; free(p); // Obvious Crash, but I need how it works and why cra...