Access Violation when accessing Dynamic Array

tsbockman via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jan 3 05:55:13 PST 2016


On Sunday, 3 January 2016 at 03:41:12 UTC, Jack wrote:
> I didn't actually knew it works that way because last time I 
> tried using '==' directly it won't compare.

Well, there are always many ways that things can go wrong 
(including compiler bugs), but it SHOULD work, so ask on the 
forums if it doesn't.

> Yeah I did that in a panic attempt to fix the problem.

Ah yes. I've been there before, myself...

> I always thought that associative arrays are much more faster 
> and more clean than this. Thanks I'll keep that in mind

The associative array itself is quite fast, as long as the data 
set doesn't have too many hash collisions. But, the lookup as a 
whole is limited by the speed of the key type's hash code 
function, and of its comparison function.

For, say, an integer, both the hash code and the comparison can 
be accomplished with about two instructions. Thus, a 
value_type[int] array would be very fast.

However, you are using `string` values as the keys, which means 
that getting the hash code or doing a comparison involves looping 
over each character in the `string`. Obviously this is going to 
be slower.

This is not to say it's actually *slow* - don't feel bad about 
using an associative array with `string` keys if that's really 
what you need. (And you're likely to need one for *something* 
since you're writing a parser.)

Assuming all the key values (variable names) are known at compile 
time, using boolean variables directly like I suggested is 
(nearly) the fastest method possible, because the `string` lookup 
is done during compilation, and is translated into a simple 
memory access or register access in the generated assembly code.

> And this is news to me too. I'll keep enums in mind.
>
> Overall thank you for your help and advice. I'll seriously keep 
> these in mind.

You're welcome.


More information about the Digitalmars-d-learn mailing list