www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 622] New: There should be a warning for unininitalized class reference

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

           Summary: There should be a warning for unininitalized class
                    reference
           Product: D
           Version: 0.175
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: andkhropov mtu-net.ru


This error is very common for people with C++ background (I've already seen
people asking questions about similar code several times):
----------------------------------------------
class Foo{
  public int data;
}

void main()
{
  Foo f; // obviously they try do this way as they got used to it in C++
         // should be a warning here!

  f.data = 1;// Access violation
}

----------------------------------------------


-- 
Nov 30 2006
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=622


wbaxter gmail.com changed:

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





Yes please!  I make this mistake all the time, and every time it takes a few
minutes tracking down why and where my program is crashing.


-- 
Nov 30 2006
prev sibling next sibling parent Ary Manzana <ary esperanto.org.ar> writes:
d-bugmail puremagic.com escribió:
 http://d.puremagic.com/issues/show_bug.cgi?id=622
 
            Summary: There should be a warning for unininitalized class
                     reference
            Product: D
            Version: 0.175
           Platform: PC
         OS/Version: Windows
             Status: NEW
           Severity: enhancement
           Priority: P2
          Component: DMD
         AssignedTo: bugzilla digitalmars.com
         ReportedBy: andkhropov mtu-net.ru
 
 
 This error is very common for people with C++ background (I've already seen
 people asking questions about similar code several times):
 ----------------------------------------------
 class Foo{
   public int data;
 }
 
 void main()
 {
   Foo f; // obviously they try do this way as they got used to it in C++
          // should be a warning here!
 
   f.data = 1;// Access violation
 }
 
 ----------------------------------------------
 
 
Actually, the warning should be in "f.data = 1;". You are trying to access a member of an uninitialized variable. Take a look at this code: ---------------------------------------------- Foo f; if (something) { f = new Foo(); } else { f = new Bar(); } f.data = 1; ---------------------------------------------- (where Bar extends Foo, for example). No warning should be generated, everyhing in it's place. That's the way access to uninitialized variable in java works. They are errors, not just warnings (the program will crash there, no matter what happens).
Nov 30 2006
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=622


bearophile_hugs eml.cc changed:

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



This bug is half-fixed: the compiler is now able to catch this bug at
compile-time, but only if the code is compiled with -O.
I think the compiler has to catch this bug when not optimized mode is used too.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 12 2010
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=622


Andrei Alexandrescu <andrei metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
                 CC|                            |andrei metalanguage.com
         AssignedTo|nobody puremagic.com        |bugzilla digitalmars.com


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 26 2010