Skip navigation.

Software Development

Correcting The Future

October 2009

( Monthly archive )

What If... C++ Pointer Notation Was Different?

A long time ago... on this very blog, I was trying to create a new programming language that fit a certain set of requirements. We all know this ended up with Project V. But one thing from back then keeps me wondering "what if?". In my theoretical programming language that never was, I had a different notation for pointers.

(Note: I had touched on the subject a while back, but I can't find it. So I'm posting about it again.)

Currently in C and C++, you use the star notation. Each star you use is a dereference. And each ampersand is getting the address of the variable in question. But each of those operators works on whatever type that has been declared for the variable. So getting the address of a pointer variable will get you an address to that pointer (which then points to a variable). In essence, you have to keep track of where you are with dereferences.

What if it worked slightly differently? Declaring pointer variables would still be the same. But what if no star always meant accessing the variable directly regardless of the pointer type. Using one star would get the address of the variable. Two stars would get the address of a pointer and so on.

What this would mean is that you would not need two operators. You would need only one. Dereferencing will be done as required by the compiler all the way down to the variable itself. Getting an address can be done up to one level higher than the declared pointer type.

int a,b;
int *var; // pointer variable.
int **ptr2; // pointer to pointer variable.

// Note the same amount of *'s in the line below.
*var = *a; // set var pointer to address of a.
var = 5;
a = 10; // Overwrites 5.

*b = *a; // error

**ptr2 = **var;
ptr2 = 15; // overwrites 10 in a.


Since a and b aren't pointers, you can only go one level up. IOW, you can use one star, but not two. With var, you can use zero, one or two stars (one higher than it was declared). With ptr2, you can go up to three stars. Also, when you use the maximum allowed stars, it is read only. That's why writing to "*b" would cause an error.

I think this kind of code would be much easier to deal with. When a function needs a pointer, you know that the variable needs one star. No questions asked. Something else that would happen is that arrays would simply be accessed by index into the list of variables. The compiler probably shouldn't allow a[1] for example. It would technically be valid syntax. But the declaration would tell the compiler that the extra items haven't been allocated. To get access to a pointer in an array of pointers, you would do *ptr2[index]. Again, no star means data and one star means a pointer, etc.

The real reason behind all this is to keep things consistent. With pointers, you would now essentially be creating aliases. And you don't need to know all the weird details of pointer arithmetic for most tasks. "*var = *a" would become the standard way to set an alias. Now you can use 'var' as if it was 'a' itself, except it's done through a pointer. In fact, you can create aliases at any level such as is the case with "**ptr2 = **var". Since ptr2 aliases to the pointer of 'var', it also aliases to the variable of 'a'. This is why you can do "ptr2 = 15;" which is the same as "a = 15" by the compiler doing a double dereference.

What's more, the ampersand isn't used to get the address of a variable anymore. So the current way that C++ handles aliasing could be kept as is. In fact, the ampersand would only be used for this style of aliasing. When combined with the new star notation, you would have to use the same amount of stars as found in the declaration.

int& *var2 = *var; // OK
int& var3 = var; // Error, 'var' was declared as '*var' with one star.


Passing arguments by reference would use this same restriction though temporaries can be created by the compiler as is currently the case.

An example using arrays.

int array[1024];

int *ptr = *array[0];
int total=0;
for(int i=0;i<1024;i++,(*ptr)++)
{
  total+=ptr;
}
cout << total;


You could also do funky stuff like set up a triangular array using a vertical pointer to pointer array and then allocate each horizontal array independently. When the triangular (or irregular) array is set up, you access it like a normal array "total+=triarray[row][column]". This is presently impossible without using pointer syntax such as "(*triarray[row])[column]".

If someone were to implement everything described here, would there be any glaring disadvantages or perhaps something that wouldn't work? I wonder what the C and C++ world would be like if this syntax were used instead. Maybe it would be minor. I don't know. But I can't help but believe that it would have made pointers much easier to deal with.

(edit: Wanted to mention that declarations would also make more sense. The way it is now in C and C++, you can assign a pointer in the declaration even though there is a star which normally indicates a dereference.)

(edit2: One problem I've noticed is that you wouldn't be able to dereference a pointer returned from a function. You'd have to store it and then use it. Unless the programmer can decide what pointer level he wants to use. So func() would try to access the variable no matter what. *func() would try to access the pointer. That would work I guess.)

Writing Multithreaded Programs Can't Be Done In C - Spolsky

Found this via reddit.

I'll quote it here for those that don't want to go to the link.

Spolsky: Quite probably. I mean C is a car, it’s very dangerous – it doesn’t have seatbelts, but it’s very powerful because it goes very fast. In fact, I’d go so far as to say, unless some people are gonna punch me about this one, but you cannot write multi-threaded code in the C programming language. You just can’t. Although technically all the capabilities are there, it is beyond the capability of mortal humans – and I know, some of you out there are very smart and you think that you have the capability of writing multi-threaded code in the C programming language, because you’re hot shit. Well, let me tell you, it is beyond the capability of humans on this planet, for their brains are just not adequate to the task of writing multi-threaded code in most languages, least of all low-level languages like C. It’s just not gonna work, it’s just not gonna make you happy. So there.



Emphasis mine. Those sections in bold leave me scratching my head. Spolsky is known for saying outrageous things, but I never thought he was on par with Richard Stallman and Linus Torvalds.

Is it hard to get multithreaded programming right? Sure. But Spolsky is saying that it's humanly impossible. That "it's just not gonna work, it's just not gonna make you happy. So there." Give up now.

Personally, I'm not a big fan of threads. I find too many people use them when there's no need. But those needs do arise from time to time. Blocking calls are the biggest problem. Other times, it just makes sense to separate code into their respective units that have their own execution point. For example, I once wrote a backgammon server and separated the database part from the rest of the server. I eventually put it in its own process so that it could be run on a different machine, but the principle is the same. The server and database handling run in parallel (or concurrently if you want the proper term). Why would database access need its own thread? Because when you make a request, you need to wait for the actual database program to return you a result. During that time, delays can be caused by different things like network latency if on a different machine, disk latency, processing (if using multicore), etc. So during that time, you have free processing power that is going to waste. By having another thread(s) for the main server, you can process other requests while waiting for the database to return a response.

Are there other times you need to use threads? Well, this is probably what Spolsky actually meant (though I can't be sure). That when we go to multicore, Spolsky would prefer to use functionality that is simpler to use for using all the cores such as special "parallel" commands found in many languages. These will send statements to different processors to be computed and then the "parallel" command will put the results back together in the correct order. A simple (and not very good example) might be to increment numbers in a list. No locking needed and each processor can increment their respective ranges within the list.

Are there ways to simplify your life when programming multithreaded programs in C or C++? Yeah. I've talked about this many times. Over and over again, I've promoted the idea of having systems or global modules that only talk to other modules through data messages (without a function call). In effect, each module essentially acts like a separate process. But you have the advantage that you have shared memory and message passing can happen so much easier and simpler. So if you ever need to extract the module into it's own process, it's no big deal. Also, any globally shared data would simply be put into a module of its own. And then I've often talked about helper classes in C++ to help insert and extract data from those messages. But that's another topic.

The biggest problem is taking something that isn't multithreaded and then trying to turn it that way after that fact. That'll cause a lot of problems because the foundation isn't built for it. Hence my promotion of self executing modules because you can expand and add more at any time.

So why would someone say multithreading is impossible? I don't know. But it made me laugh. A lot! At first, I thought he was just saying it was hard. But no. He's saying flat out impossible. Strange stuff.

edit: I wonder what Spolsky thinks of P2P software like uTorrent (written in C++). Or browsers and servers (Apache for example uses many processes and was written in C), Operating Systems, or anything distributed. Anyways, I just don't get what he was going for.

Parallel vs. Concurrent

I'm seeing more of these debates on Reddit, especially as functional programmers try to redefine what these words mean. I'm reading comments from instructors who are teaching the incorrect meaning. Having been blasted time and again when I started this blog about terminology, I made sure to use the correct words, but parallel and concurrent have never been a problem.

When speaking in general terms, both can be used interchangeably in many cases. The expression "running in parallel" is especially troublesome because it simply means that multiple things are executing at the same time. There's a reason for this use, but we can back up a little and start simple.

Parallel usually means several instances of the same thing executing at the same time (usually independently of each other).
Concurrency usually means several things executing at the same time and working together toward a common goal.

Read more...

December 2009
S M T W T F S
November 2009January 2010
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31