Showing results for Code - The Old New Thing

Aug 18, 2014
Post comments count0
Post likes count0

Deleting elements from an JavaScript array and closing up the gaps

Raymond Chen
Raymond Chen

Today's Little Program is an exercise that solves the following problem: Given a JavaScript array and an unsorted array (possibly containing duplicates), calculate the result of deleting all of the elements from the original array as specified by the indices. For example, suppose and . The indices specify that elements 2 (charles), 4 (eve...

Code
Aug 15, 2014
Post comments count0
Post likes count0

The scope of the C# checked/unchecked keyword is static, not dynamic

Raymond Chen
Raymond Chen

C# has operators and to control the behavior of the language in the face of integer overflow. There are also and statements which apply the behavior to blocks of statements rather than single expressions. Why, then, doesn't this code below raise an overflow exception? (Mini-exercise: Why couldn't I have just written ?) The answer is th...

Code
Aug 14, 2014
Post comments count0
Post likes count0

Customers not getting the widgets they paid for if they click too fast -or- In C#, the += operator is not merely not guaranteed to be atomic, it is guaranteed not to be atomic

Raymond Chen
Raymond Chen

In the C# language, operation/assignment such as are explicitly not atomic. But you already knew this, at least for properties. Recall that properties are syntactic sugar for method calls. A property declaration is internally converted to the equivalent of Accessing a property is similarly transformed. Note that the only operations you c...

Code
Aug 13, 2014
Post comments count0
Post likes count0

Keep your eye on the code page: C# edition (the mysterious third code page)

Raymond Chen
Raymond Chen

A customer was having trouble manipulating the console from a C# program: We found that C# can read only ASCII data from the console. If we try to read non-ASCII data, we get garbage. Observe that this program is unable to read the Å and ö characters. They come back as garbage. Although there are three code pages that have spec...

Code
Aug 12, 2014
Post comments count0
Post likes count0

Keep your eye on the code page: C# edition (warning about DllImport)

Raymond Chen
Raymond Chen

Often, we receive problem reports from customers who failed to keep their eye on the code page. Does the function support files with non-ASCII characters in their names? We find that the function either fails outright or returns question marks when asked to provide information for files with non-ASCII characters in their name. If we ask for...

Code
Aug 11, 2014
Post comments count0
Post likes count0

If you want to set a thread's apartment model via Thread.CurrentThread.ApartmentState, you need to act quickly

Raymond Chen
Raymond Chen

Welcome to CLR Week 2014. Don't worry, it'll be over in a few days. A customer wanted to know why their was displaying the infamous Current thread must be set to single thread apartment (STA) mode before OLE calls can be made error. "Even though we set the to , the apartment state is still . Curiously, if we put the above code in a standalo...

Code
Aug 8, 2014
Post comments count0
Post likes count0

The case of the orphaned LpdrLoaderLock critical section

Raymond Chen
Raymond Chen

A customer found that under heavy load, their application would occasionally hang. Debugging the hang shows that everybody was waiting for the critical section. The catch was that the critical section was reported as locked, but the owner thread did not exist. How can a critical section be owned by thread that no longer exists? There are two...

Code
Aug 7, 2014
Post comments count0
Post likes count0

Since clean-up functions can't fail, you have to soldier on

Raymond Chen
Raymond Chen

Clean-up functions can't fail, so what do you do if you encounter a failure in your clean-up function? You have to keep cleaning up. Some people like to follow this pattern for error checking: And some like to put it inside a cute flow control macro like or even Whatever floats your boat. But you have to be careful if using this p...

Code
Aug 6, 2014
Post comments count0
Post likes count0

Why does Explorer say "File too large" for my custom file system, when the problem has nothing to do with the file being too large (heck it's not even a file)

Raymond Chen
Raymond Chen

When Explorer copies files around, it doesn't really know what the maximum file size supported by any file system happens to be. (That information is not reported by .) So it guesses. If the file system name is or , then Explorer assumes that the maximum file size is 4GB − 1. Also, if a file operation fails with the error , and Explorer ca...

Code
Aug 4, 2014
Post comments count0
Post likes count0

Enumerating the ways of choosing teams in a group of players

Raymond Chen
Raymond Chen

Suppose you have a bunch of people, and you want to break them up into m teams of size n. (Therefore you have a total of nm people.) Today's Little Program will enumerate the ways this can be done. Formally, let's say that you have a collection of size nm, and you want to enumerate the ways of partitioning the collection into m subsets, each sub...

Code