static keyword

Discussion in 'C++' started by Frank-O, Jan 10, 2007.

  1. Frank-O

    Frank-O Guest

    class A
    {
    int dCol;
    public :
    const char * Display(void) const { return this==NULL ? "A" : "B" ; }

    };

    int main(void)
    {

    static A * a;
    printf(" a is ", a->Display());

    return 0;

    }

    it prints A

    I don' t understand why the pointer value is necessary NULL ?
     
    Frank-O, Jan 10, 2007
    #1
    1. Advertising

  2. Frank-O

    Ron Natalie Guest

    Frank-O wrote:
    > class A
    > {
    > int dCol;
    > public :
    > const char * Display(void) const { return this==NULL ? "A" : "B" ; }
    >
    > };
    >
    > int main(void)
    > {
    >
    > static A * a;
    > printf(" a is ", a->Display());
    >
    > return 0;
    >
    > }
    >
    > it prints A
    >
    > I don' t understand why the pointer value is necessary NULL ?
    >

    because the default initialization of a pointer is to zero intialize
    it. Since this is a static sorage object, it does get default initialized.

    However, your program has UNDEFINED behavior. You are not guaranteed
    of anything when you dereference a null pointer.
     
    Ron Natalie, Jan 10, 2007
    #2
    1. Advertising

  3. Frank-O

    Kai-Uwe Bux Guest

    Frank-O wrote:

    > class A
    > {
    > int dCol;
    > public :
    > const char * Display(void) const { return this==NULL ? "A" : "B" ; }
    >
    > };
    >
    > int main(void)
    > {
    >
    > static A * a;
    > printf(" a is ", a->Display());
    >
    > return 0;
    >
    > }
    >
    > it prints A


    It may print anything: "a->Display()" dereferences an uninitialized pointer
    variable. The program has undefined behavior.


    > I don' t understand why the pointer value is necessary NULL ?


    There is nothing to understand: your program has undefined behavior;
    anything can happen.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Jan 10, 2007
    #3
  4. Frank-O

    Dennis Jones Guest

    "Kai-Uwe Bux" <> wrote in message
    news:eo3h2o$oam$...
    > Frank-O wrote:
    >
    >> class A
    >> {
    >> int dCol;
    >> public :
    >> const char * Display(void) const { return this==NULL ? "A" : "B" ; }
    >>
    >> };
    >>
    >> int main(void)
    >> {
    >>
    >> static A * a;
    >> printf(" a is ", a->Display());
    >>
    >> return 0;
    >>
    >> }
    >>
    >> it prints A

    >
    > It may print anything: "a->Display()" dereferences an uninitialized
    > pointer
    > variable. The program has undefined behavior.


    I do not believe that is true. a->Display() is not actually "dereferencing"
    the pointer. The Display() method exists regardless of whether or not any
    instances of 'A' are created, and it will get invoked via a->, regardless of
    the value of 'a'. That is, the ability to call methods in the class is not
    dependent on having a valid, non-NULL pointer. Of course, what happens if
    the method attempts to make use of data members (like 'dCol' in the example)
    within the class, since the 'this' pointer is uninitialized (or NULL in this
    case), is completely unknown.

    For example, if Display() were written thus:

    const void * Display(void) const { return this; }

    Then:

    A *a = (A *)0x12345678;
    printf("a is %p", a->Display());

    ....should print: "a is 12345678" without any difficulty, even though the
    memory at the address assigned to 'a' is likely to contain garbage, because
    it makes no use of the 'this' pointer (which is silently passed to the
    function). However, the following would probably crash:

    const void * Display(void) const { dCol = 6; return this; }

    because it is attempting to modify memory that it very likely does not own,
    which would probably cause an access violation or core dump.

    I suppose it is possible that calling a method through an invalid pointer
    may result in undefined behavior in some implementations, but on every
    compiler I have used, class methods are always available and callable
    through uninitialized and NULL pointers -- you just have no way of knowing
    what will happen if you try to access memory through the 'this' pointer.

    - Dennis
     
    Dennis Jones, Jan 10, 2007
    #4
  5. Dennis Jones wrote:

    > the pointer. The Display() method exists regardless of whether or not any
    > instances of 'A' are created, and it will get invoked via a->, regardless
    > of the value of 'a'. That is, the ability to call methods in the class is
    > not dependent on having a valid, non-NULL pointer.


    Maybe this is valid for your compiler, but is not a rule of the language.

    > Of course, what happens if the method attempts to make use of data members
    > (like 'dCol' in the example) within the class, since the 'this' pointer is
    > uninitialized (or NULL in this case), is completely unknown.


    The difference is just that the probability of crash when doing this in any
    given compiler is higher that in the other case. The correctness is the
    same: none.

    --
    Salu2
     
    =?ISO-8859-15?Q?Juli=E1n?= Albo, Jan 10, 2007
    #5
  6. Frank-O

    Dennis Jones Guest

    "Julián Albo" <> wrote in message
    news:...
    > Dennis Jones wrote:
    >
    >> the pointer. The Display() method exists regardless of whether or not
    >> any
    >> instances of 'A' are created, and it will get invoked via a->, regardless
    >> of the value of 'a'. That is, the ability to call methods in the class
    >> is
    >> not dependent on having a valid, non-NULL pointer.

    >
    > Maybe this is valid for your compiler, but is not a rule of the language.


    Granted.

    - Dennis
     
    Dennis Jones, Jan 11, 2007
    #6
  7. Frank-O

    David W Guest

    "Dennis Jones" <> wrote in message
    news:QKdph.18035$Pe7.5726@trnddc04...
    >
    > "Kai-Uwe Bux" <> wrote in message
    > >
    > > It may print anything: "a->Display()" dereferences an uninitialized
    > > pointer
    > > variable. The program has undefined behavior.

    >
    > I do not believe that is true. a->Display() is not actually "dereferencing"
    > the pointer.


    As far as the language is concerned, it is.

    > The Display() method exists regardless of whether or not any
    > instances of 'A' are created, and it will get invoked via a->, regardless of
    > the value of 'a'. That is, the ability to call methods in the class is not
    > dependent on having a valid, non-NULL pointer.


    Depends on the compiler/hardware. It may be that on a given machine the pointer
    value will be placed in a register that must hold a valid address, for access by
    the member function as 'this'. In any case, what the C++ standard is all that
    matters.

    DW
     
    David W, Jan 11, 2007
    #7
  8. Frank-O

    Salt_Peter Guest

    Dennis Jones wrote:
    > "Kai-Uwe Bux" <> wrote in message
    > news:eo3h2o$oam$...
    > > Frank-O wrote:
    > >
    > >> class A
    > >> {
    > >> int dCol;
    > >> public :
    > >> const char * Display(void) const { return this==NULL ? "A" : "B" ; }
    > >>
    > >> };
    > >>
    > >> int main(void)
    > >> {
    > >>
    > >> static A * a;
    > >> printf(" a is ", a->Display());
    > >>
    > >> return 0;
    > >>
    > >> }
    > >>
    > >> it prints A

    > >
    > > It may print anything: "a->Display()" dereferences an uninitialized
    > > pointer
    > > variable. The program has undefined behavior.

    >
    > I do not believe that is true. a->Display() is not actually "dereferencing"
    > the pointer. The Display() method exists regardless of whether or not any
    > instances of 'A' are created, and it will get invoked via a->, regardless of
    > the value of 'a'. That is, the ability to call methods in the class is not
    > dependent on having a valid, non-NULL pointer. Of course, what happens if
    > the method attempts to make use of data members (like 'dCol' in the example)
    > within the class, since the 'this' pointer is uninitialized (or NULL in this
    > case), is completely unknown.
    >
    > For example, if Display() were written thus:
    >
    > const void * Display(void) const { return this; }
    >
    > Then:
    >
    > A *a = (A *)0x12345678;
    > printf("a is %p", a->Display());
    >
    > ...should print: "a is 12345678" without any difficulty, even though the
    > memory at the address assigned to 'a' is likely to contain garbage, because
    > it makes no use of the 'this' pointer (which is silently passed to the
    > function). However, the following would probably crash:
    >
    > const void * Display(void) const { dCol = 6; return this; }
    >
    > because it is attempting to modify memory that it very likely does not own,
    > which would probably cause an access violation or core dump.
    >
    > I suppose it is possible that calling a method through an invalid pointer
    > may result in undefined behavior in some implementations, but on every
    > compiler I have used, class methods are always available and callable
    > through uninitialized and NULL pointers -- you just have no way of knowing
    > what will happen if you try to access memory through the 'this' pointer.
    >
    > - Dennis


    The above doesn't make sense. Display() is a *member function*, not a
    global function or a free-standing function. Whether or not the member
    function is const implies that a valid entity is available. The fact
    that you aren't accessing a particular object's member is only an
    accident here.

    In such a case, noone cares if it works on some particular compiler or
    platform. Or that it works on everyday except Sundays. Its undefined
    behaviour because it can't be guarenteed. Whether, where and when it
    works is irrelevent.

    Next you'll be suggesting that a loose/unitialized/null pointer is a
    valid target for a pseudo-object.
     
    Salt_Peter, Jan 11, 2007
    #8
  9. Frank-O

    David W Guest

    "David W" <> wrote in message
    news:ljfph.25753$...
    >
    > In any case, what the C++ standard


    says

    > is all that matters.


    DW
     
    David W, Jan 11, 2007
    #9
  10. Frank-O

    Dennis Jones Guest

    "Salt_Peter" <> wrote in message
    news:...
    >
    > The above doesn't make sense. Display() is a *member function*, not a
    > global function or a free-standing function. Whether or not the member
    > function is const implies that a valid entity is available. The fact
    > that you aren't accessing a particular object's member is only an
    > accident here.
    >
    > In such a case, noone cares if it works on some particular compiler or
    > platform. Or that it works on everyday except Sundays. Its undefined
    > behaviour because it can't be guarenteed. Whether, where and when it
    > works is irrelevent.
    >
    > Next you'll be suggesting that a loose/unitialized/null pointer is a
    > valid target for a pseudo-object.


    No, I was only saying that with the compilers I've tried, the function table
    for a class exists, even if an instance on the class does not. I do not
    condone the use of code in this manner -- I only suggest that it is possible
    to do (at least on the compilers I've tried), and easily explains the
    behavior the OP asked about.

    - Dennis
     
    Dennis Jones, Jan 11, 2007
    #10
  11. Frank-O

    Grizlyk Guest

    Dennis Jones wrote:

    > > Frank-O wrote:
    > > static A * a;


    Must be initialized at least to NULL, else you get access to random
    memory area due to "a" can be placed to uninitialized memory area
    filled with random values.

    > > printf(" a is ", a->Display());


    > I do not believe that is true. a->Display() is not actually "dereferencing"
    > the pointer. The Display() method exists regardless of whether or not any
    > instances of 'A' are created, and it will get invoked via a->, regardless of
    > the value of 'a'.


    If member Display() is virtual, it can not be called for NULL pointer,
    because class of real object can not be found.

    > I suppose it is possible that calling a method through an invalid pointer
    > may result in undefined behavior in some implementations, but on every
    > compiler I have used, class methods are always available and callable
    > through uninitialized and NULL pointers -- you just have no way of knowing
    > what will happen if you try to access memory through the 'this' pointer.


    What is the reason to call member for NULL pointer? I think any
    compiler can treat the calling as error, similar to stack overflow
    check and other runtime checks, and compiler do not check for NULL
    pointer only for perfomance requirements.
     
    Grizlyk, Jan 14, 2007
    #11
  12. Frank-O

    Guest

    Frank-O wrote:
    > class A
    > {
    > int dCol;
    > public :
    > const char * Display(void) const { return this==NULL ? "A" : "B" ; }
    >
    > };
    >
    > int main(void)
    > {
    >
    > static A * a;
    > printf(" a is ", a->Display());
    >
    > return 0;
    >
    > }
    >
    > it prints A
    >
    > I don' t understand why the pointer value is necessary NULL ?


    Incidentally, there is a pop song called "Monster" by The Automatic
    which includes the line "You can't avoid static abuse". Presumably this
    is what they were thinking of when they wrote it... :)
     
    , Jan 14, 2007
    #12
  13. Frank-O

    Guest

    Dennis Jones wrote:
    > "Kai-Uwe Bux" <> wrote in message
    > news:eo3h2o$oam$...
    > > Frank-O wrote:
    > >
    > >> class A
    > >> {
    > >> int dCol;
    > >> public :
    > >> const char * Display(void) const { return this==NULL ? "A" : "B" ; }
    > >>
    > >> };
    > >>
    > >> int main(void)
    > >> {
    > >>
    > >> static A * a;
    > >> printf(" a is ", a->Display());
    > >>
    > >> return 0;
    > >>
    > >> }
    > >>
    > >> it prints A

    > >
    > > It may print anything: "a->Display()" dereferences an uninitialized
    > > pointer
    > > variable. The program has undefined behavior.

    >
    > I do not believe that is true. a->Display() is not actually "dereferencing"
    > the pointer. The Display() method exists regardless of whether or not any
    > instances of 'A' are created, and it will get invoked via a->, regardless of
    > the value of 'a'. That is, the ability to call methods in the class is not
    > dependent on having a valid, non-NULL pointer.


    Suppose A contained one or more virtual functions. Wouldn't the
    compiler be likely to read the object, in order to find a vtable?

    Or are you relying on the compiler spotting that there are no virtual
    functions and so not attempting to use the vtable mechanism?
     
    , Jan 14, 2007
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?RGF2ZQ==?=

    Static keyword usage in asp.net?

    =?Utf-8?B?RGF2ZQ==?=, Nov 18, 2005, in forum: ASP .Net
    Replies:
    11
    Views:
    5,510
    =?Utf-8?B?RGF2ZQ==?=
    Nov 29, 2005
  2. Matt
    Replies:
    3
    Views:
    1,144
    aatishp
    Mar 15, 2012
  3. cppaddict
    Replies:
    2
    Views:
    372
    cppaddict
    May 2, 2004
  4. Replies:
    6
    Views:
    490
    Peter Otten
    May 10, 2007
  5. Hamilton, William

    RE: keyword checker - keyword.kwlist

    Hamilton, William, May 10, 2007, in forum: Python
    Replies:
    4
    Views:
    379
Loading...

Share This Page