about pointer syntax

bearophile bearophileHUGS at lycos.com
Thu Oct 14 16:27:08 PDT 2010


spir:

> (Hope you don't mind if I send other bits of code to be improved that way?)

Feel free to show them, if I am busy other people may give you an answer. The worst that may happen is that no one answers you.


> Right. I think at keeping explicit defaults like "int element = 0" for documentation.

Not putting a value, unless it's different from the standard initializator, is a very common idiom in D. It's easy to remember the inits: zero, empty string, NaN, null, invalid Unicode chars.


> If I ever write a lib for public use, I'll follow such styling guidelines ;-)

The D style:
http://www.digitalmars.com/d/2.0/dstyle.html


> Wouldn't have ever thought that the "stepping" statement of a for loop can be something else as incrementation.

This is a kind of incrementation:
symbol = symbol.next;

In C-derived languages this line of code:
symbol = symbol + 5;
May be written:
symbol += 5;

In the same way you may think of this (this is not D syntax):
symbol .= next;
As a compact version of:
symbol = symbol.next;


> Right, guess you mean while(symbol != null)?

Right. But some people don't like that.


> I need to explore this further (what's the actual purpose of "auto").

It just asks the compiler to use the default type, it performs a bit of local type inferencing.


> For curiosity, I intend to benchmark lists vs sequential arrays vs associative arrays, for various element counts.

Good, it's a way to understand the language better. In the lists vs dynamic array I suggest you to also take a look at the produced (dis)assembly.


> to know what kind of data structures were suited for symbol tables representing the content of record-like objects, which number of entries is typically small since hand-written by the programmer: sophisticated structures like associative arrays and tries started to perform better than plain sequences for counts >> 100.)<

D dynamic arrays are not bad, but they are not very efficient, so a sequential scan in a small array of integer numbers is faster than an hash search.

You may also try a "perfect hash", for your purpose. Around you may find C code (that's easy to translate to D) to create a perfect hash out of a sequence of strings.

A binary search in an array of strings-int pairs is an easy option too.

If your symbol are names are short you may also put your string+int pairs in a single flat data structure (even keeping a fixed length for each one of them), to increase CPU cache locality a bit, something like:

struct Pair { char[10] name; int element; }
Pair[20] data;

In C/C++/D languages usually there are *many* complicated ways to speed up code :-) You generally want to use them only in the spots where the profiler tells you you need performance. The profiler is used with the DMD -profile switch.

It's also good if you learn D to start using unittests and design by contract from the beginning. They help a lot avoid bugs. And the more hairy (pointers, etc) your code is, the more useful they are.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list