Function call problem

I have a program where there is a class and i call a function of the class in main:
#include <iostream>
using namespace std;

class Class {
     virtual void fun(char* ch) { cout << "Class::fun " <<ch<< endl; }

typedef void (*Fun)(char*);

int main() {
     Class objClass;

     cout << "Address of virtual pointer " << (int*)(&objClass+0) << endl;
     cout << "Value at virtual pointer i.e. Address of virtual table " 
           << (int*)*(int*)(&objClass+0) << endl;
     cout << "Value at first entry of virtual table " 
           << (int*)*(int*)*(int*)(&objClass+0) << endl;

     cout << endl << "Executing virtual function" << endl << endl;
     Fun pFun = (Fun)*(int*)*(int*)(&objClass+0);
     return 0;

If the function fun of class Class had no argument then their is no error, but since there is an argument(char* type) and I am sending the parameter hello to fun. There is a run time error of chkesp.c. Please help me.

Who is Participating?
DanRollinsConnect With a Mentor Commented:
Teaching to fish:

1) Change your code to look like this:

class Class {
    virtual void fun(char* ch) { cout << "Class::fun " <<ch<< " this:" << this << endl; }
typedef void (*Fun)(char*);
char sHello[]= "hello";
Class objClass;
int main() { sHello );
    pFun( sHello );
    cout << "Done" << endl;
    return 0;

2) Place a breakpoint on"hello");
3) run the program.  At breakpoiunt use command View/Debug Windows/Disassembly.  

This will show you what is different between the call via the vtable and the 'normal' call to the virtual member fn.  For clarity, I turned off code optimzation on an otherwise 'Release' build.  As you can see...

29: sHello );
68 B0 70 42 00       push        offset sHello (004270b0)
B9 34 B3 42 00       mov         ecx,offset objClass (0042b334)
E8 B4 00 00 00       call        Class::fun (00401220)
31:       pFun( sHello );
68 B0 70 42 00       push        offset sHello (004270b0)
FF 55 FC             call        dword ptr [pFun]
83 C4 04             add         esp,4

... there are only two differences:  The 'normal' call sets ECX to point ot the object (it is a requirement of the 'thiscall' calling convention) and the 'indirect' call does not do that, but DOES pop the stack (add esp,4) to discard the pointer to sHello that it pushed (this is a requirement of the 'cdecl' calling convention).

This is fascination to me ),maybe I don't get out enough)  So I tried some variations.  If the pointer is declared like this:

   typedef void (__stdcall *Fun)(char*);

then the compiler will generate:
    31:       pFun( sHello );
    68 B0 70 42 00       push        offset sHello (004270b0)
    FF 55 FC             call        dword ptr [pFun]

because it has been instructed NOT to cleanup the stack after the call.  That is what we want because objClass.Fun already cleans up the stack.  However, ECX will contain a random value going into the function.  That will result in a problem if objClass.Fun calls any other members of its class (it won't be able to find its variables or vtable).  You can only correct that by setting ECX manually:

    __asm mov ECX, offset objClass
    pFun( sHello );

Isn't learning new stuff fun?

-- Dan
I'm not sure what you're trying to do.
However, every method (function of the class) has an implicit first parameter which is a pointer to the self object (reserved keyword: this).
The only class member functions that do not have such a parameter are static member functions.


#include <stdio.h>

typedef void (*Fun)(int);

class CMyClass
    typedef void (CMyClass::*pvf) (int);

    CMyClass (void)  {}
    void exec(void);

    void func_1(int i);
     static void func_2(int i);

    pvf pFuncPtr;     // Function called to execute

void CMyClass::func_1(int i)
     printf("func_1 called with %d\n", i);

void CMyClass::func_2(int i)
     printf("func_2 called with %d\n", i);

void CMyClass::exec(void)
     printf("exec called\n");

int main (void)
    CMyClass cObj;
     cObj.pFuncPtr = CMyClass::func_1;

     Fun pFun = CMyClass::func_2;

    return 0;
Good job, Dan!!!!!!


Dear manas_ghosal

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to accept


comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points. The link to the Community Support area is:

You have 18 open questions out of 23 that need your attention! Please take
some time and accept an answer if an expert was able to help you or
provide feedback if needed.

You still have questions open dating back to the year 2000.  ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if they are still open in 14 days.  Experts, please post closing recommendations before that time.

Below are your open questions as of today.  Questions which have been inactive for 21 days or longer are considered to be abandoned and for those, your options are:
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> YOU CANNOT DELETE A QUESTION with comments; special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.

Click you Member Profile to view your question history and please keep them updated. If you are a KnowledgePro user, use the Power Search option to find them.  

Questions which are LOCKED with a Proposed Answer but do not help you, should be rejected with comments added.  When you grade the question less than an A, please comment as to why.  This helps all involved, as well as others who may access this item in the future.  PLEASE DO NOT AWARD POINTS TO ME.

To view your open questions, please click the following link(s) and keep them all current with updates.

*****  E X P E R T S    P L E A S E  ******  Leave your closing recommendations.
If you are interested in the cleanup effort, please click this link 
POINTS FOR EXPERTS awaiting comments are listed in the link below
Moderators will finalize this question if in @14 days Asker has not responded.  This will be moved to the PAQ (Previously Asked Questions) at zero points, deleted or awarded.
Thanks everyone.
Moderator @ Experts Exchange
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.