D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 4521 - Array-wise assignment on unallocated array is accepted
Summary: Array-wise assignment on unallocated array is accepted
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other All
: P2 normal
Assignee: No Owner
URL:
Keywords: accepts-invalid
Depends on:
Blocks:
 
Reported: 2010-07-27 11:46 UTC by Andrej Mitrovic
Modified: 2010-07-28 10:35 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Andrej Mitrovic 2010-07-27 11:46:44 UTC
The following code should raise an out of bounds error:

import std.stdio;

void main() {
   double[] c;
   c[] = 4;
}
Comment 1 nfxjfg 2010-07-27 22:37:00 UTC
Maybe I'm missing something, but no it should not.
"c[] = 4;" just sets every array element to 4, and it works even if there are 0 array elements.
Comment 2 Don 2010-07-27 23:57:22 UTC
(In reply to comment #1)
> Maybe I'm missing something, but no it should not.
> "c[] = 4;" just sets every array element to 4, and it works even if there are 0
> array elements.

Yes. Some related situations are definitely bugs though. Perhaps this one should be marked as a duplicate of bug 2547.
Comment 3 bearophile_hugs 2010-07-28 04:19:38 UTC
This is not a bug, I think this can be closed.
Comment 4 Andrej Mitrovic 2010-07-28 05:55:29 UTC
But isn't there a difference between arrays that had all of their elements removed and arrays that have not yet been allocated in the first place? 

I filed it since Walter seems to have confirmed this:
http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=114041
Comment 5 Andrej Mitrovic 2010-07-28 06:38:07 UTC
Actually I think I'm confusing myself with how dynamic allocation works. I thought the dynamic array always have to be called with new, but it appears I can change the length of an array without calling new in the first place, e.g.:

    int[] a;
    a.length = 4;
    a[] = 4;
    writeln(a);   // writes 4 4 4 4

So this should probably get closed. Sorry for the confusion.