www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5997] New: Static arrays with 0 length accepted by compiler

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5997

           Summary: Static arrays with 0 length accepted by compiler
           Product: D
           Version: unspecified
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Keywords: accepts-invalid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: andrej.mitrovich gmail.com



19:25:14 PDT ---
Is there a use case for 0-length static arrays?

If there's not a use-case, then this should probably be a compiler error:

int[0] logs;  // <- disallow this

void main()
{
    logs = [4];
}

The assignment statement causes a linker error (if you comment it out you won't
get any linker errors):
/+
test.obj(test)  Offset 002DFH Record Type 009D
 Error 16: Index Range
--- errorlevel 1
+/

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 13 2011
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5997


Vladimir <thecybershadow gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |thecybershadow gmail.com



---

 Is there a use case for 0-length static arrays?
I believe they can be used as a value type in an associative array to create a set. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
May 13 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5997


bearophile_hugs eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs eml.cc



One use case are variable-length structs:

struct MyArray(T) {
  size_t len;
  T data[0];

  // access methods here, that use data.offsetof
}

You create such array with a C malloc or GC malloc.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 14 2011
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5997


Andrej Mitrovic <andrej.mitrovich gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID



13:21:39 PDT ---


 Is there a use case for 0-length static arrays?
I believe they can be used as a value type in an associative array to create a set.
Ah, you're right. I even forgot about opening this topic which mentioned this trick: http://www.digitalmars.com/d/archives/digitalmars/D/A_case_for_valueless_AA_s_133165.html I'm closing this down. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
May 14 2011