Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Function call problem

Posted on 2002-04-06
Medium Priority
Last Modified: 2010-04-02
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.

Question by:manas_ghosal
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 6922662
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.


Expert Comment

ID: 6922689

#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;
LVL 49

Accepted Solution

DanRollins earned 400 total points
ID: 6923201
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() {
    objClass.fun( sHello );
    pFun( sHello );
    cout << "Done" << endl;
    return 0;

2) Place a breakpoint on    
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:       objClass.fun( 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
LVL 11

Expert Comment

ID: 6957241
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: http://www.experts-exchange.com/commspt/

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.


Expert Comment

ID: 6979256
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.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question