Jump to content
  • Advertisement
Sign in to follow this  

[C++] Pointer to function in a struct - crash [solved]

This topic is 3703 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, Probably me being stupid...couldn't find what was wrong. I have a pointer to a function which I store in a structure. At some point in the program I call the function. This is the code:
struct Item
{
    void (*function)();

    Item() : function(NULL) {}        
};

struct Object
{
    std::vector<Item> items;

    Object() {}
};

std::vector<Object> objects;

void run_function(int o, int i)
{
    // everything works till here
    objects[o].items.function(); // crashes, does not do anything in execute()
}

void execute()
{
    std::cout << "Function is running\n";
}

int main()
{
    // initialise the vector etc etc
    objects[o].items.function = execute;

    run_function(int some_o, int some_i);
}

What am I missing? Thank you, Decrius [Edited by - Decrius on March 31, 2008 4:14:45 PM]

Share this post


Link to post
Share on other sites
Advertisement
Define "it crashes".

Sounds like you're reading past the end of the allocated memory. Are you sure that objects has at least o+1 elements in it, and it's items vector has at least i+1 elements in it?

Share this post


Link to post
Share on other sites
The application returns 3...it just crashes/stops running.

Yes, I guess I'm allocation something I do not control. And I'm sure the O and I are fine, since its in a for loop which will only loop through the objects/items available.

Share this post


Link to post
Share on other sites
What are you trying to pull exactly, and where is o being declared exactly, because i could not see anywhere in the code, except for the fact you are calling it withing main. i would definitely like to know what he value of o is.

Share this post


Link to post
Share on other sites
Quote:
Original post by Decrius
The application returns 3...it just crashes/stops running.

Yes, I guess I'm allocation something I do not control. And I'm sure the O and I are fine, since its in a for loop which will only loop through the objects/items available.
If it returns 3 then it's not crashing. A crash would be it spitting out an access violation or similar.

I suggest using the debugger to step through the code to determine what throws the error. I suggest reading This if you're not familiar with the debugger (Assuming Visual Studio of course, if you're not using it then use the debugger with whatever IDE you have).

Share this post


Link to post
Share on other sites
I use CodeBlocks ;) (ofcourse hehe)

It says, Segmentation Fault, call stack says this:

Nr Address Function
#0 00000000 ??() (??:??)

Does that mean its a NULL pointer?

Share this post


Link to post
Share on other sites
Apparently it is.

I fixed the problem, though I want to add security for this.

How can I check if its a NULL pointer?

if (objects[o].items.function())
{
objects[o].items.function();
}

Doesn't seem to work =/

Thanks anyways :)

Share this post


Link to post
Share on other sites
Quote:
Original post by jpetrie
It's possible. Is there a reason why you won't post your real code?


Yes, its long and irrelevant. I thought the code I posted was wrong...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!