Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > static keyword

Reply
Thread Tools

static keyword

 
 
Grizlyk
Guest
Posts: n/a
 
      01-14-2007
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.

 
Reply With Quote
 
 
 
 
gw7rib@aol.com
Guest
Posts: n/a
 
      01-14-2007

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...

 
Reply With Quote
 
 
 
 
gw7rib@aol.com
Guest
Posts: n/a
 
      01-14-2007

Dennis Jones wrote:
> "Kai-Uwe Bux" <(E-Mail Removed)> wrote in message
> news:eo3h2o$oam$(E-Mail Removed)...
> > 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?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
put static keyword inside the JSP scripplets error Matt Java 3 03-15-2012 10:10 AM
RE: keyword checker - keyword.kwlist Hamilton, William Python 4 05-13-2007 06:31 AM
keyword checker - keyword.kwlist tom@finland.com Python 6 05-10-2007 04:53 PM
Static keyword usage in asp.net? =?Utf-8?B?RGF2ZQ==?= ASP .Net 11 11-29-2005 07:11 PM
static keyword inside member function cppaddict C++ 2 05-02-2004 07:09 PM



Advertisments