4

I have a interface documented like this:

typedef struct Tree {
  int a;
  void* (*Something)(struct Tree* pTree, int size);
};

Then as I understand I need to create instance of it, and use Something method to put the value for 'size'. So I do

struct Tree *iTree = malloc(sizeof(struct Tree));
iTree->Something(iTree, 128);

But it keeps failing to initialize. Am I doing this right? Howcome the first member of the Something method is pointer to the very same struct?

Can anyone explain please?

Thanks

6
  • There is not much C++ in this question. You might consider removing the tag or get answers like Ben's.
    – pmr
    Jul 5, 2011 at 13:23
  • 1
    I can't get one thing: what's the purpose of that typedef?
    – sidyll
    Jul 5, 2011 at 13:27
  • 1
    You will want to remove the typedef unless you name the structure by moving Tree to the end between } and ;. ^ @sidyll +1 Your comment showed up after I posted this.
    – Joe
    Jul 5, 2011 at 13:27
  • No problem @Joe, and thanks by the way. I thought it would be some special addition from C++ , as it doesn't make much sense.
    – sidyll
    Jul 5, 2011 at 13:32
  • @pmr: I'd give the same answer to the c tag: It's emulating a C++ virtual function without language support.
    – Ben Voigt
    Jul 5, 2011 at 14:03

3 Answers 3

8

You have to set Something to something since it is only a function pointer and not a function. The struct you created with malloc just contains garbage and struct fields need to be set before it is useful.

struct Tree *iTree = malloc(sizeof(struct Tree));
iTree->a = 10; //<-- Not necessary to work but you should set the values.
iTree->Something = SomeFunctionMatchingSomethingSignature;
iTree->Something(iTree, 128);

Update

#include <stdlib.h>
#include <stdio.h>

struct Tree {
    int a;
    //This is a function pointer
    void* (*Something)(struct Tree* pTree, int size);
};

//This is a function that matches Something signature
void * doSomething(struct Tree *pTree, int size)
{
    printf("Doing Something: %d\n", size);
    return NULL;
}

void someMethod()
{
    //Code to create a Tree
    struct Tree *iTree = malloc(sizeof(struct Tree));
    iTree->Something = doSomething;
    iTree->Something(iTree, 128);
    free(iTree);
}
2
  • Keep in mind a function pointer is just that, a pointer. It needs to reference a function somewhere. Jul 5, 2011 at 13:24
  • :/ I don't understand... what is SomeFunctionMatchingSomethingSignature ??
    – Pett
    Jul 5, 2011 at 13:27
4

This is a poor man's virtual function. The initial parameter is roughly equivalent to C++'s this pointer in a member function. And you must manually set the function pointers before calling them, whereas C++ virtual functions are set up by the compiler.

7
  • 1
    This doesn't really answer the question. While it's marked C/C++, it's clearly a C question since it's failing. Jul 5, 2011 at 13:23
  • can you please give me example? Thanks!
    – Pett
    Jul 5, 2011 at 13:24
  • @Dylan: What part of you must manually set the function pointers before calling them doesn't answer the question?
    – Ben Voigt
    Jul 5, 2011 at 14:01
  • I mean, it seems superfluous to mention the comparison to a C++ virtual method, because, it's not. It's a C function pointer in a C structure. Jul 5, 2011 at 14:03
  • @Dylan: Which is what you use when you want a virtual function, but you're stuck with C.
    – Ben Voigt
    Jul 5, 2011 at 14:05
1

The member Tree::Something is never initialized. You allocate space for a Tree, but allocation is different from initialization, and your allocated Tree contains only unmeaningful bits.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.