I use Visual Studio 2012 Express to debug a 64-bit app. Let's say both 'foo' and 'bar' are some member functions of a class C.
'foo' looks like:
void foo() {
bar(); // change to this->bar() works!
}
My program crashed because 'this' pointer is changed when it went inside 'bar'. The problem can be fixed by changing to 'this->bar()'.
Any idea how I should debug this problem? Thanks
You mean that the following code works?
void foo() {
this->bar(); // instead of bar()
}
So, presuming foo() is a member function that calls another member function bar() for the same object, check two things: (1) whether there is another non-member function bar() having same signature of member function bar(), if so exist, change the non-member function name op always qualify with this-> for member function call as a good programming practice (2) When you call foo() for some object instance or pointer to object instance, check whether the object has been allocated and initialized properly.
Related
The c++ standard (ISO c++11) mentions in Section 9.3.1 that
A non-static member function may be called for an object of its class
type, or for an object of a class derived (Clause 10) from its class
type, using the class member access syntax (5.2.5, 13.3.1.1).
An attempt to compile this code with g++ (version 4.8.2)
class foo{
public:
void bar(){
cout<<"hey there"<<endl;
}
};
int main(){
foo obj;
foo::bar(&obj);
}
gives a compile time error because it couldn't match the function's signature. I guess that is expected given what the standard states about calling member functions.
Since the method will eventually take a form similar to bar(foo*) during some stage of compilation, why does the standard asks for member access syntax to call the member function?
Lets add a static member to the class as:
class foo{
public:
void bar() { cout<<"hey there"<<endl; }
static void bar(foo*) { cout<<"STATIC MEMBER"<<endl; }
};
Now if you write this:
foo::bar(&obj); //static or non-static?
Which function should be called? In such situation, how would you call both of them? What would be the syntax? If you allow one function to have this syntax, you've to abandon it (i.e syntax) for other function. The Standard decided to have foo::bar(&obj) syntax for static member function, while abandoning it for non-static member function.
Anyway, if you want to pass &obj as argument to the non-static member function, then you can use type-erasure facilitated by std::function as:
void (foo::*pbar)() = &foo::bar; //non-static member function #1
std::function<void(foo*)> bar(pbar);
bar(&obj); //same as obj.bar();
Likewise, you could call static member function as:
void (*pbar)(foo*) = &foo::bar; //static member function #2
std::function<void(foo*)> bar(pbar);
bar(&obj); //same as foo::bar(&obj);
Note that at lines #1 and #2, the types of the object pbar makes the compiler to choose the correct member function — in the first case, it takes the pointer to the non-static member-function while in the latter case, it takes the pointer to the static member function.
Hope that helps.
Basically, I have an already compiled a file that is written assembly; .obj file. It has an exported function f. In C++, I wrote a class and want to use the exported function f as a member function.
If it were to be used as a global function, I know that one just writesextern "C" f(). However, this doesn't work with a member function, at least I didn't figure out how to do it.
So, how do I do it?
Note: The function f is written properly. i.e. it takes into account the this pointer, etc. It should work correctly at assembly level.
If the function is written as a free function, you will have to declare it as a free function the way you're accustomed to. Just as you can't build a library with void foo() and then somehow declare it as void bar() and expect it to work, you can't take a free function and turn it into a member function. What you can do, though, is add an inline forwarder into your class like so:
struct S;
extern "C" void f( S * );
struct S {
void f() {
using ::f;
f(this);
}
};
That's the best you can do, as far as I'm aware.
I have a function that resides in a class in an application my objective is to inject a dll into the target proccess and call that member function by its address.
Here is the function:
void GameAudio::StopAllSounds(void) // this is at address 0x004A0656
I have tried calling it like this
typedef void(__stdcall * caller)(void);
caller call = (caller)0x004A0656;
I have used everything: __stdcall , __cdecl, __thiscall you name it.
Class's non-static member function signature is different than the normal function.
For example,
class X {
public:
void foo ();
};
void foo ();
In above case X::foo() and ::foo() are different from each other. Remember that, X::foo() under the hood is somewaht like:
void X::foo (X* const this);
^^^^^ implicitly added
Thus what you are attempting to do would result in an undefined behavior.
If you insists to call the member function using its address, then better to make it a static member function.
class X {
public:
static void foo ();
};
void foo ();
Signature for X::foo() and ::foo() are same.
Edit: From your comment, it seems that you don't have the control over the source code. I would suggest to use the proper calling convention for the function signature instead of hard coded address. Pseudo code
typedef void (GameAudio::*caller_type)();
caller_type caller = &GameAudio::StopAllSounds;
GameAudio object;
(object.*caller)();
There is no syntax in C++ for what you are doing.
Note that the function could even be virtual, and so you'd be calling the wrong function.
The easiest way is to change it/ into/add a non member function, or a static member function, with an additional GameAudio* parameter. That function could be easily be called using a pointer-to function.
If you still want to call the member function directly your best bet is probably to use assembler. The problem then is the portability: your solution will depend on the compiler, SO, architecture and maybe even the ABI version.
void func() {assert(0);}
int main () {void func();}
The above code does not call func(), or at least does not reach the assertion. Not that I really need to know, but I'm just curious, what is going on here?
You're declaring a prototype for a function named func which returns nothing and takes no arguments. That's (one of) the subtle difference between function calls and function prototypes. Notice that the line above main, the void func() {assert(0);}, has no effect on whether this is a prototype or a call. You could remove it and the code would do the same thing - that is, nothing.
This also tells you that you can redeclare function prototypes. You could even have this:
int main() {
void blah();
void blah();
void blah();
void blah();
}
And the code would still do what it did before - nothing.
If you leave off the void, it would call the function.
Also, notice that in the case of a function which takes parameters, this:
int main() { func(4); }
would not turn into a prototype if you added void before it like this:
int main() { void func(4); }
it would just produce a syntax error.
As others have pointed out, the line
void func();
inside of main is treated as a function prototype rather than a call to the function func. In C and C++, you can declare function prototypes inside of functions if you wish, though it's rarely done in practice.
The fact that this is legal causes all sorts of headaches for programmers. For example, if you rewrote the code as
(void) func();
Then this would compile as a call to func whose return type is explicitly casted to void to indicate "I don't care about this return value." In other words, this set of parentheses changes the declaration into a statement.
In C++, this problem can be compounded by the fact that this code below is a function prototype, not a variable declaration invoking the default constructor:
Object myObject();
Though
Object myObject(137);
does create the object and pass 137 into its constructor, and
Object myObject;
creates the object without calling the constructor.
There is an awful edge case of the language called the "most vexing parse" that arises when trying to declare an object while calling its constructor. For example, this code is legal C++, but it's a function declaration rather than a variable declaration:
set<int> mySet(istream_iterator<int>(cin), istream_iterator<int>());
The problem is that this could be parsed as a function declaration rather than a creation of an object that accepts two temporary istream_iterator<int>s as parameters. To fix this, in C++ you'd have to write
set<int> mySet(istream_iterator<int>(cin), (istream_iterator<int>()));
Where, as above, the extra parentheses forcibly disambiguate the statement from being a function prototype to being a declaration.
Hope this helps!
You can declare functions, even when it's unnecessary. That's what you've done, re-declared the function.
You are declaring a local function void func() inside main().
The void statement indicates the compiler that it is a declaration and not a function call. So, remove the void, your function will be called.
What does it means when I in my code declare something like :
Foo * foo();
What benefits it gives me ?
Ist not simple pointer variable as you see .
Also
What does it mean if I declare it in the c++ source above all methods for example :
// Test cpp class
.
#include "Foo.h"
Foo * foo();
Test::Test() {}
Test::GetFooMethods()
{
foo()->someMethod();
}
…
…
Foo * foo(); is a declaration to a function named foo which returns a pointer to a Foo object. It is not a function pointer.
The code Foo()->someMethod(); is probably a typo. You probably meant foo()->someMethod();. In this case, the code calls foo() which as stated above returns a pointer to a Foo object, and then you call the Foo::someMethod() method through that pointer.
If in fact you did mean Foo()->someMethod(); things get a little crazy. What this means is first a Foo object is default initialized on the stack. Then Foo::operator->() is called, returning some object which has a method named someMethod(), and that function is called. See why I guess that it was a typo? :)
EDIT:
To answer the last question in your post, when you declare a function without defining it, you are telling the compiler, "I've got a function named foo that takes no parameters and returns a pointer to a Foo, but I'm not going to tell you how that function is implemented yet." This allows you to write code that uses that function and the compiler will be happy. The linker however will be quite grumpy with you if you do not someplace provide a definition for this function.
Foo * foo();
This is not a function pointer. This is a declaration of a function named foo that takes no arguments and returns a pointer to a Foo.
A pointer to a function of this type would look like:
Foo* (*foo_ptr)() = foo;
Just like
int getX();
is a declaration of a function that returns an int, so too
Foo * foo();
is a declaration of a function that returns a Foo pointer;
What does it means when I in my code
declare something like : Foo * foo();
Foo * foo();
Very tough to say without knowing what is Foo.
If Foo is a type e.g. struct Foo{};, then this declares a function named 'foo' which returns a pointer to some type called Foo. This looks to be the case in your post based on your usage subsequently (and also the title of the post).
If Foo is a #define e.g. #define Foo 1, then this is not a declaration but an expression statement which multiplies 1 by the return value of 'foo' in case operator * is defined for the types of the operands involved.
What benefits it gives me ?
Tough to say again without knowing what Foo is.
Also What does it mean if I declare it
in the c++ source above all methods
Declaring 'foo' (if it is indeed a declaration) as high as possible in the translation unit means larger the tentative scope of the name 'foo'.
void f1(){g();} // not OK, 'g' not known
void f(){}
void g(){f();} // OK, f() is known
BTW, does Foo (caps F) have an overloaded operator ->?