Disallow arrays as pointers
bearophile
bearophileHUGS at lycos.com
Sun Oct 30 14:29:36 PDT 2011
I have translated C code similar to:
something foo(int n) {
int *array = malloc(n * sizeof(int));
int i;
for (...) {
for (i = 0; i < n; i++, array++) {
*array = ...;
*array = ...;
}
...
}
Into D2 code similar to:
something foo(int n) {
auto array = new int[n];
size_t index;
foreach (...) {
foreach (i; 0 .. n) {
array[index] = ...;
*array = ...;
index++;
}
...
}
Do you see the bug I have found during the debugging?
I think the D2 compiler has to catch this bug:
*array = ...;
D arrays aren't pointers. Letting the compiler see them as pointers is bug-prone, not tidy, and doesn't help D newbies understand how D represents its arrays.
My bug report:
http://d.puremagic.com/issues/show_bug.cgi?id=3990
Some very good comments about this topic:
http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=135391
Bye,
bearophile
More information about the Digitalmars-d
mailing list