473,473 Members | 1,842 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Address of static method

Hi,
my compiler tells me that the address of a static method will always
evaluate to true (which is 1). Why that ? How can i get the address of a
static method ?
I'm using gcc3.

Regards
Thorsten

May 19 '06 #1
8 4843
Thorsten Kiefer wrote:
Hi,
my compiler tells me that the address of a static method will always
evaluate to true (which is 1). Why that ? How can i get the address of a
static method ?
I'm using gcc3.


It should only evaluate to true if you are testing it. Otherwise it
should simply be a non-zero address. Please post a minimal but complete
sample of code that demonstrates the problem (cf.
http://parashift.com/c++-faq-lite/ho....html#faq-5.8).

Cheers! --M

May 19 '06 #2
mlimber wrote:
Thorsten Kiefer wrote:
Hi,
my compiler tells me that the address of a static method will always
evaluate to true (which is 1). Why that ? How can i get the address of a
static method ?
I'm using gcc3.


It should only evaluate to true if you are testing it. Otherwise it
should simply be a non-zero address. Please post a minimal but complete
sample of code that demonstrates the problem (cf.
http://parashift.com/c++-faq-lite/ho....html#faq-5.8).

Cheers! --M


thread.hpp :
#include <pthread.h>
#include <iostream>
namespace std {

class Thread {
protected:
pthread_t pthread;

static void *start_routine(void *x);
static void test() {};

public:
Thread(){
cout << &start_routine << endl;
cout << &test << endl;
int r = pthread_create(&pthread,0,start_routine,this);
}
virtual int run() = 0;
};

}

threadtest.cpp :
#include <thread.hpp>
#include <iostream>
using namespace std;
class Thread1 : public Thread {
public:
int run() {
for(int i = 0;i < 10;++i)
cout << i << endl;
}
};
int main(int argc,char **argv){
Thread1 t1;
}
Result:
1
1
Segmentation fault
Greets
tk

May 19 '06 #3
Thorsten Kiefer wrote:
mlimber wrote:
Thorsten Kiefer wrote:
Hi,
my compiler tells me that the address of a static method will always
evaluate to true (which is 1). Why that ? How can i get the address of a
static method ?
I'm using gcc3.


It should only evaluate to true if you are testing it. Otherwise it
should simply be a non-zero address. Please post a minimal but complete
sample of code that demonstrates the problem (cf.
http://parashift.com/c++-faq-lite/ho....html#faq-5.8).

Cheers! --M


thread.hpp :
#include <pthread.h>
#include <iostream>
namespace std {

class Thread {
protected:
pthread_t pthread;

static void *start_routine(void *x);
static void test() {};

public:
Thread(){
cout << &start_routine << endl;
cout << &test << endl;
int r = pthread_create(&pthread,0,start_routine,this);
}
virtual int run() = 0;
};

}

threadtest.cpp :
#include <thread.hpp>
#include <iostream>
using namespace std;
class Thread1 : public Thread {
public:
int run() {
for(int i = 0;i < 10;++i)
cout << i << endl;
}
};
int main(int argc,char **argv){
Thread1 t1;
}
Result:
1
1
Segmentation fault
Greets
tk


This is neither a minimal nor a complete program -- the pthread
business is non-standard and unnecessary here to demonstrate your
problem, and you don't define Thread::start_routine() anywhere. Also,
you didn't tell us where the warning message was issued, but I checked,
and it's on each of the couts in Thread::Thread(). Before I get to
that, however, you may not add things to the std namespace, so get
Thread out of there. Anyway, it looks like that's a bug in g++ 3. It
doesn't happen on VC++ 6, 2003 (online), 2005, EDG (online) or Comeau
(online). Better ask in a gnu group. See this FAQ for some
possibilities:

http://www.parashift.com/c++-faq-lit...t.html#faq-5.9

Cheers! --M

May 19 '06 #4
Hi,
ok i prepared anither example:

test.cpp:
#include <iostream>

using namespace std;
struct Test {
static void f();
};

void Test::f(){
}

int main(int argc,char ** argv){
cout << &Test::f << endl;
}

compile :
g++ -O3 -funroll-loops -fomit-frame-pointer -march=athlon-xp -lstdc++
test.cpp -o test
test.cpp: In function ‘int main(int, char**)’:
test.cpp:14: warning: the address of ‘static void Test::f()’, will always
evaluate as ‘true’

output :
1
What's the ouput with VC++ ?

Regards
Thorsten

May 19 '06 #5
Thorsten Kiefer wrote:
Hi,
ok i prepared anither example:

test.cpp:
#include <iostream>

using namespace std;
struct Test {
static void f();
};

void Test::f(){
}

int main(int argc,char ** argv){
cout << &Test::f << endl;
}

compile :
g++ -O3 -funroll-loops -fomit-frame-pointer -march=athlon-xp -lstdc++
test.cpp -o test
test.cpp: In function 'int main(int, char**)':
test.cpp:14: warning: the address of 'static void Test::f()', will always
evaluate as 'true'


You program is converting a non-null function pointer to a bool value
when it streams the function pointer's value to std::cout - so the
compiler is right: the value resulting from the conversion will always
be true.

To obtain the address of a global or static function, either use its
name alone or its name appended by the "address of" operator (&).

So either:

Test::f

or

&Test::f

specifies the address of the Test::f() static function.

Greg

May 19 '06 #6
Thorsten Kiefer wrote:
cout << &Test::f << endl;


There is no stream inserter for a pointer-to-member-function, so the
compiler converts &Test::f to a Boolean value and shows that. Since the
pointer isn't null, the value is true.

--

Pete Becker
Roundhouse Consulting, Ltd.
May 19 '06 #7
Pete Becker wrote:
Thorsten Kiefer wrote:
cout << &Test::f << endl;


There is no stream inserter for a pointer-to-member-function, so the
compiler converts &Test::f to a Boolean value and shows that. Since the
pointer isn't null, the value is true.

Hi,
thanks for your answers.
cout << (void *)&Test::f << endl;
works fine.

In my Thread-program i got a Segmentation fault, and I thought it's because
the address of the static member function is 1. But the reason is that i
forgot to link libpthread.so.

Greets
Thorsten

May 20 '06 #8
Thorsten Kiefer wrote:
thanks for your answers.
cout << (void *)&Test::f << endl;
works fine.


No, it doesn't. It just happens to give an answer that looks like it
makes sense. There is no requirement that a pointer to member function
(static or otherwise) be convertible to void*.

--

Pete Becker
Roundhouse Consulting, Ltd.
May 20 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: cppsks | last post by:
Taking the address of a static const resulted in a unresolved symbol. Why is that? Is the address assigned at load time? Thanks.
1
by: Henrik Nyberg | last post by:
Here's a small method for validating email in C#. It may save you some time.. public static bool IsValidEmailAddress(string sEmail) { if (sEmail == null) { return false; } int nFirstAT =...
0
by: comp.lang.php | last post by:
I wrote a method that should check if an email address is valid. In another method I've already checked to see if $_POST exists and is well-formed, so those checks are not necessary in this scope....
6
by: Todd A. Anderson | last post by:
I have a function foo of which I need to get the address. The problem is that when you say "&foo" (or just foo for that matter), you get the address of this function's entry in a jump table and...
4
by: Paul Jansen | last post by:
Don't go away yet... this is a bit more complicated than 'how do I take a method address'! I have a class foo, and method foo::bar. I also have a tree of foo objects, which I process...
2
by: Allen | last post by:
// 1. RPCMethodRegistry.h typedef int (*RPCMethod)(CRPCParaPacker& packer); class CRPCMethodEntry { public: int id; RPCMethod method; };
4
by: =?Utf-8?B?QWxleCBLLg==?= | last post by:
Hi all I need a simple program that allows me to check if an IP address is pingable. I am not going to send/receive anything to the remote host, just check if it is visible. Something like...
6
by: Nicolas Noakes | last post by:
Hello, I would like to convert to following process to code. Any advice is welcome. I have a hardware device which requires the this procedure to set it's IP address. First create an static...
7
by: e2point | last post by:
hi, i got a program that is suppose to run 24x7x365. However after functioning for around 15 minutes, it crashes due to a segmentation fault. program is written in c++ and runs in RH Linux 4. When...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.