I’ll toss two ideas out.

1. I’ve got a book by John Robbins, (2003) “Debugging Applications for Microsoft .NET and Microsoft Windows”. Yon about page 667, he discusses the Debug C Run-Time Library and Memory Management. It has built in support to find leaks with both new/delete and malloc/free. It’s an old book, and I don’t know whether this functionality still exists.

2. You asked about how to make sure “that any particular non-trivial static object’s lifetime would persist beyond the last call to the allocation operators we are trying to track.” One way might be to make a trivial static object like an array of bytes, and then allocate the non-trivial object on top of it. You can do this with the placement variant of the operator new.

Caveat: I haven’t tried any of this.

]]>See here for a great introduction to generating functions, in particular the definition at the bottom of page 7 for this notation. Note the continuing discussion on the following page 8, which gets to your next question: we could write the “generating function portion” of the formula for p(n,d) in two different ways: (n!)^2[x^(2n)]blah^d, meaning, “write out blah^d, extract the coefficient of x^(2n), and multiply the result by (n!)^2”; or equivalently, [x^(2n)/(n!)^2]blah^d, meaning, “write out blah^d, and extract the coefficient of x^(2n)/(n!)^2.” See the equation (1.2.8) and subsequent example in the link.

Your by-hand calculation of the probability 20/64 for your example with n=3 and d=2 is correct; to use the generating function approach, if we evaluate just the polynomial sum((x^k/k!)^2 for k=0..3)^2, we get (1+x^2+x^4/4+x^6/36)^2, which expanding yields 1+2*x^2+3/2*x^4+5/9*x^6+17/144*x^8+x^10/72+x^12/1296. The coefficient of x^6 is 5/9, times (3!)^2 is 20, divided by 2^6 is 20/64. Alternatively, the coefficient of x^6/((3!)^2) is 20, divided by 2^6 is 20/64.

One final note: this isn’t really a *probability* generating function. There are such things, where *each* coefficient is a probability of the corresponding value of an integer random variable (thus, the coefficients sum to 1). In this case, only the coefficient of x^(2n) (including the additional factors (n!)^2/d^(2n)) is a probability.

]]>But, I still don’t understand how you arrived at p(n,d). I do get that you want to take the coefficient of the X**2n term, but think that the (sum(k=0 to n of X**k / k!)**d would have already given that and don’t see why another factor of x**2n sits outside the sum.

I’m also still not clear on why n! squared is in the denominator rather than numerator. I thought you were essentially treating it as multinomial and would have expected n! squared to be in the numerator (as it ends up in your code).

Suppose we have a cheap knock off of Skittles that has only d=2 colors red and green and you only get n=3 Skittles in a pack. There are 4 possibilities for what you will get in a randomly selected pack. 1/8 of the time, you’ll get 3 red. 1/8 of the time, you’ll get 3 green. 3/8 of the time you’ll get two red and one green and 3/8 of the time you’ll get two green and one red. Square each of these and sum for the probability that two randomly selected bags are the same. 1/64 + 9/64 + 9/64 + 1/64 = 20/64.

Now, let’s use the probability generating function. we have 1/2**(2*3) * (1/3!)**2 * [1 + 2x**2 + x**4 + (8/6)x**6 + (7/12)x**8 + (1/12)x**10 + (1/36)x**12] if I did the math right and ignoring the leading x**2n term that I doubt.

Taking the coefficient of X**6 should give us the probability of randomly selecting two packs with 3 skittles of two possible colors. So that is (1/64) * (1/36) * (8/6) = 1/1728. That’s pretty far off from 20/64.

If I un-ignore the leading x**2n term, and multiply by it, then well be taking the constant term from the power of the sum factor. This will always be 1. In that case, the probability would be (1/64) * (1/36) * 1 = 1/2304. This is also pretty far from 20/64.

So, maybe you could comment on my interpretation and even better, explain how you derived the probability generating function.

]]>2. Imagine arranging the candies in two equal-size packs in a row, with the first half of the row containing the n candies in the first pack, and the second half containing those in the second pack. In the generating function (i.e., polynomial in formal variable x), each power of x corresponds to a particular *total* number 2n of candies in the row. (And the coefficient indicates the number of possible such arrangements of two n-candy packs with the same color composition.)

3. Suppose, temporarily, that there are exactly M different compositions of Skittles packs, and that they are all equally likely (i.e., the probability that two randomly selected packs are identical is 1/M). Then a very good approximation for the expected number of packs until we first encounter a duplicate is sqrt(pi/2 x M) (see here). In this case, different types of packs are not equally likely, but we can extend this approximation by letting M=1/q, where q is the probability of two identical packs, so that our estimated average is sqrt(pi/(2q)).

]]>Three questions:

1. In the expression for p(d, n), are you sure that n! belongs in the denominator?

2. What is the function of X**2n in p(d, n)?

3. Can you elaborate on the “back-of-the-envelope square-root estimate”?