Archive for October, 2010

const

It took me a long time to understand that the const qualifier in C is overloaded. There was no const when I first learned C. It was introduced in the C90 standard. It was copied from C++, although the meaning of const is subtly different in the two languages.

The first meaning of const in C is to use it to qualify a variable definition. If you do that, it means that the variable can not be changed; it is immutable. It can have an initializer but will never have another value. A global or static const variable may be placed in read-only memory.

The second meaning is to use it to qualify a pointer. In this usage it means that the program may not change the value being pointed to by using the pointer. However, it does not mean that that value is immutable, as it may be changed by a different pointer, or by a function call, or by casting away the const qualifier.

The first meaning of const has a real effect on the program. A variable declared const may be compiled differently from a variable which is not declared const. The second meaning of const, on the other hand, is, in effect, compiler-enforced documentation. The compiler will issue an error if an attempt is made to change a value using a const qualified pointer, but declaring such a pointer will not change the generated code.

Although const qualified pointers are just documentation, in practice they are used far more often than const variables. When the C90 standard came out and C compilers started supporting const, programmers spoke of const-poisoning: the feeling that once you use the const qualifier anywhere, it spread throughout your program as it had to be tracked through all assignments and function calls to avoid compiler warnings. Adding const-poisoning to a program does not make it any more correct or reliable. It’s just documentation, albeit documentation that the compiler enforces.

One can imagine a range of different types of documentation which it might be nice to have the compiler enforce. Perhaps it would be useful to know the alignment of a generic pointer, or to know that this char* pointer points to a null terminated string while that one points to a block of arbitrary bytes. A program can use a typedef to indicate the intended meaning of some value, but a pointer to one typedef can be assigned to a pointer to a different typedef with no warning if the underlying types happen to be the same. Why do we get compiler warnings for const pointers but not for typedef pointers? It’s because const was added to the type system, whereas typedef, despite its name, is merely an aliasing mechanism, and is not part of the type system at all.

It did not have to be this way. It would have been possible to make const a storage specifier, like static, rather than a type qualifier. It would have simply meant that the variable was immutable. The address of a const int would have type int * rather than const int *.

The C90 standard introduced one other type qualifier: volatile (in fact, the standard introduced the whole idea of type qualifiers). Unlike const, the standard did not define the meaning of a volatile qualified pointer. The address of a volatile variable has a volatile qualified pointer type, but the standard never said what the compiler should do with such a type. It only says that any access to a volatile variable must be through an lvalue with the volatile qualifier.

The C99 standard adds a third type qualifier, restrict, which is another can of worms.

In retrospect I think that adding the const qualifier to the type system was a mistake. It lets the compiler enforce a specific type of documentation but doesn’t let the program define the types of documentation that it cares about. The documentation is not reliable as programs can cast away the qualifier, and indeed the standard requires basic functions like strchr to do exactly that.

If you want the compiler to be able to check type attributes, then give the language a way to define the attributes that matter to a particular program. If you want to compiler to be able to put a variable in a specific type of storage, such as a read-only data section, then use a storage specifier. There is no need to mix the two ideas.

Comments (2)

Water

Today is Blog Action Day and the topic for the year is water. Access to fresh water is an increasing problem around the world as the combination of increasing population and climate change leave more people without it. Many people get their fresh water from glacial runoff or from aquifers, and the one is melting away and the other is being drained.

Of course there is no shortage of water in the world. There isn’t even a shortage of fresh water. The problem is one of getting the fresh water where the people are; moving people somewhere else is mostly infeasible.

Jerry Pournelle had a series of short stories in the ’70s which involved moving icebergs to get fresh water for irrigation. I always thought that was a great idea. You would have these huge icebergs slowly moving across the oceans. Unfortunately, I don’t think it pans out. Icebergs are really heavy to move around the ocean, and moving them outside of the ocean is impossible. Any place you can move an iceberg, you have plenty of water; it’s just not fresh. The energy it takes to move an iceberg would be more efficiently used doing desalination. Pournelle’s stories weren’t completely crazy when it wrote them, but desalination technology has come a long way, and the physics of moving an iceberg is never going to change.

The successful use of desalination in places like the United Arab Emirates and Israel shows that the problem of access to fresh water is one of energy and infrastructure. It’s not really a typical scarce resource problem. The problem for the world is the large number of poor people with decreasing fresh water supplies. Transporting water is expensive and requires a lot of infrastructure work, particularly if you have to do it uphill. Poor people have no money to spend on doing it. Nobody is likely to do it for them.

The most efficient way to get people water is likely going to be to significantly increase reuse, and to shift agriculture to areas where water can be provided more cheaply. Increasing reuse also requires infrastructure, but it’s cheaper to build than long pipelines. Shifting agriculture has its own set of issues, but it’s likely to be easier to move food than to move people.

The last thing I have to say about water concerns privatization. There is no substitute for water, and it is needed daily, so people will pay any price to get it. Creating alternative water distribution mechanisms is extremely expensive, and to my knowledge has never been done. Putting a private company in charge of water distribution means granting a monopoly for a price inelastic good to a profit seeking entity. There is an enormous incentive to jack up prices as high as the market will bear, which is to say almost all the money available. There is little incentive to invest heavily in new infrastructure, since the company is already absorbing all the available money. That is admittedly the extreme version of what can happen, but really it can not end well, and in general, when it has been done, it has not ended well. Privatization can work well when it creates a competitive market. Water distribution is never a competitive market. Of course there are roles for private companies in the water system, but only in some sort of public/private partnership.

Comments (7)

Proposition 23

Last summer’s bizarre California ballot initiative was proposition 16, a PG&E funded measure which was fortunately voted down. This season’s appears to be proposition 23. Proposition 23 is admittedly much less crazy than proposition 16: it calls for the California Global Warming Solutions Act of 2006 to be suspended until California’s unemployment rate drops to 5.5% or below for four consecutive quarters. The Global Warming Solutions Act requires that greenhouse gas emission levels in the state be cut to 1990 levels by 2020.

What’s bizarre about proposition 23 is that the monetary supporters are primarily oil companies based out of state. Their argument is that the Global Warming Solutions Act will cost jobs. That argument makes little sense. Working to reduce greenhouse gas emissions creates jobs, as people must work to develop and implement new technologies. The argument is that this will lead to increased costs for businesses, and that they will pass the costs on to consumers. That is most likely correct, but what it means is that money is shifted around. It doesn’t mean that money or jobs is lost, although certainly some specific people will gain and some will lose.

So the question is how the money would shift. And the answer is that the money would shift away from technologies which generate greenhouse gases and toward technologies which do not. The former are found in places which generate fossil fuels, such as Texas. The latter are found in places which do green technology research and development, such as Silicon Valley. So the overall effect of the law is most likely to cause more of the money that California residents spend on energy to stay in California rather than to move to places like Texas. Or, for that matter, Saudi Arabia. It is clear why so many out of state oil companies are supporting proposition 23.

Now, I happen to think that there is a straightforward case to be made for why we don’t want to increase greenhouse gases in the atmosphere, and the counter-arguments to that do not make sense to me. But I am of course aware that it is, for reasons that continue to escape me, a controversial topic. What is, however, not controversial is that there is a limited amount of oil on this planet, and that the most pointless possible thing we can do with that limited oil is burn it. Moreover, the U.S. is a net oil importer, and the money we send overseas to purchase oil is being sent directly to countries which do not share our values and often work against our interests. I believe that any rational calculation would lead us to decrease our use of oil, which in turn means decreasing our generation of greenhouse gases.

So I see exactly one argument in favor of proposition 23: it supports the profits of oil companies. And I see many reasons against it. I sincerely hope the proposition is defeated.

Comments (10)

China Money 2

I’ve been trying to wrap my head around the arguments about China’s currency control. Before the financial crisis got going, it was fairly easy to understand. China controlled their currency by investing in U.S. Treasury Bonds, which had the effect of keeping U.S. interest rates low. People in the U.S. borrowed money at low rates and spent it in China, encouraging Chinese exports and discouraging U.S. exports. China siphoned off a good chunk of the money through currency controls and sent it back to the U.S. It was a virtuous cycle, except that China kept accumulating bonds.

Back then there were mutterings about the cycle, but nobody really wanted to break it. Things are different now. Interest rates in the U.S. are low, really low. The U.S. no longer needs that Chinese investment to keep interest rates down. Now people are focusing more on the part about exports and imports. As long as China controls their currency, it will be hard for the U.S. to sell into China. Of course, it would be hard anyhow, as the Chinese government controls the Chinese market in many different ways. But making it possible to have competitive prices seems like a necessary first step.

The Chinese government’s controls on currency also have the effect of making their population poorer than they would otherwise be. The money that Chinese workers earn on the world market is not available to them to spend on imports. The government can get away with this because the standard of living is still rising in China, even if it is not rising as fast as it otherwise would. There is a second effect keeping Chinese people from spending on imports, which is that China does not have the social programs that European countries and to a lesser degree the U.S. has. That means that Chinese people save money when they can, because they can not count on getting support later when they need it.

Currency controls are a restraint on free trade, but they are also standard practice for developing countries. It makes sense to use currency controls to encourage people to invest locally and build up local production capabilities. But China is the second largest economy in the world; do they still count as a developing country?

If China lets their currency rise, their exports will inevitably fall. China would risk falling into a local recession. They could use their currency reserves to cushion the bad effects on people, but it would still tend to reduce local manufacturing capabilities. The Chinese government doesn’t want that. The potential gain for China is more abstract: by letting their currency rise, they help the overall global economy, which in turn, eventually, helps China. I don’t know if the Chinese government is prepared to take that sort of short-term risk for long-term gain.

On the U.S. side, the controlled level of the Chinese currency is a trade barrier. This hurts the U.S. in the long term, as it increasingly loses manufacturing capability as people pay artificially low prices to buy goods from China. The U.S. could address this by imposing trade barriers, but that would carry a different long-term cost: U.S. manufacturing capability, protected from direct competition, would become increasingly uncompetitive. The U.S. doesn’t have any good options that I can see.

So it seems to me that the discussions about Chinese currency become pointless. The U.S. has no leverage, and China knows that. China has no incentive to let their currency rise, so they won’t do it. I can see only one way out of this: the U.S. could balance the federal budget and stop selling long-term bonds. That would give China no way to control their currency relative to the dollar, and the currencies would start to move to a more sensible valuation. Unfortunately, U.S. political paralysis makes it impossible to either raise taxes or cut benefits, so this scenario seems unlikely.

Well, there is another way that things could change: the Chinese political system could change. A more democratic government in China would be more responsive to the desires of the Chinese population, and would therefore be less inclined to keep them in relative poverty to keep the export machinery humming. But that scenario also seems quite unlikely.

So things will most likely continue as they have been, to the detriment of people in both the U.S. and China. Unfortunate, but so it goes.

Comments (8)

The Facebook Movie

I really enjoyed The Social Network, although I doubt it had much connection to the real people involved. I don’t doubt the events as such, but I doubt the interpretation.

I noticed one thing in particular. One of the motivations of the Mark Zuckerberg character was that he really wanted to get into a Finals Club. But the Finals Clubs are essentially an archaic holdout of aristocratic WASP privilege. I didn’t attend Harvard, but I did attend an Ivy League school with similar organizations. They were not taken particularly seriously by anybody I knew, except perhaps by those whose parents had belonged to one (and those people were pretty much all accepted). This is true even though I knew people who were accepted.

It’s very difficult to imagine somebody obsessing about getting into one. It’s particularly difficult to imagine that from somebody writing web programs. The path to fame and riches in the computer world is through what you do, not who you know. The Finals Clubs are naturally inclined toward the people who are going to become lawyers and politicians. Why would somebody like Zuckerberg care about a Finals Club?

Of course people’s motivations are mysterious. Zuckerberg did go to Phillips Exeter, an elite private prep school, and he could have absorbed some sort of Finals Club ethos there. Still, Zuckerberg has said publically that he was not interested in joining a Finals Club, and I’m inclined to take him at his word.

Comments (1)

« Previous entries Next Page » Next Page »