• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
TheComet

Function Pointers in Structs

7 posts in this topic

I'm developing a thing in C, and currently I have followed a style where I provide a struct declaration containing data, and separate functions for acting upon said data. Something like this:
 
MyData.h

struct MyData
{
   int a;
   float b;
};

void initialise( struct MyData* instance );
int get_a( struct MyData* instance );
float get_b( struct MyData* instance );

MyData.c

#include <MyData.h>

void initialise( struct MyData* instance )
{
    instance->a = 2;
    instance->b = 7.3f;
}

int get_a( struct MyData* instance )
{
    return instance->a;
}

float get_b( struct MyData* instance )
{
    return instance->b;
}

main.c

#include <MyData.h>

int main( void )

{

    MyData test;
    initialise( &test );
    int x = get_a( &test );
    float y = get_b( &test );

    return 0;

}

I'm wondering whether or not to do some pointer trickery to make it look more OO, like the following:

 

MyData.h

struct MyData
{
    int a;
    float b;

    int (*get_a) (struct MyData*);
    float (*get_b) (struct MyData*);
};

void initialise( struct MyData* instance );

MyData.c


#include <MyData.h>

static int get_a( struct MyData* instance )
{
    return instance->a;
}

static float get_b( struct MyData* instance )
{
    return instance->b;
}

void initialise( struct MyData* instance )
{
    instance->get_a = get_a;
    instance->get_b = get_b;

    instance->a = 2;
    instance->b = 7.3f;
}

main.c


#include <MyData.h>

int main( void )

{

    MyData test;
    initialise( &test );
    int x = test.get_a( &test );
    float y = test.get_b( &test );

    return 0;

}

The benefit I see is being make get_a and get_b static, so they can't be called directly without having instantiated a type from MyData.

What other pro's and con's are there from doing this, and am I doing the initialisation correctly for assigning the function pointers?

0

Share this post


Link to post
Share on other sites

The first approach is how normal member functions work in C++

 

The second is similar to how virtual functions work.

 

The pros and cons are roughly the same...you get some overhead in calling function pointers, but it can open the door to polymorphic "classes".

 

I can agree it looks a bit nicer, but that might be because I'm a C++ guy, and the question arises, why not just use C++? smile.png

 

 

 


The benefit I see is being make get_a and get_b static, so they can't be called directly without having instantiated a type from MyData.

 

Not sure this is a big benefit though...

You'll just have a different runtime error while calling the functions if the struct isn't correctly initialized, though I guess it might fail faster..

And wouldn't you need a pointer to the struct anyway to be able to call the function? Hopefully it would be initialized at that point if your architecture is good.

Edited by Olof Hedman
1

Share this post


Link to post
Share on other sites

As I see it, if you decide to use C you should do it only if you want to avoid being tempted as easily to create overcomplicated OOP-isms. If you want OOP you can spare yourself from emulating it and just switch the compiler to C++ and be able to use all those goodies like function overloading, namespaces, destructors, std library, exceptions and so on.

The example looks like its doing no useful work (I guess its cause of being a quick example only); theres no need for useless getters with function pointers for such a simple struct.

In C I think a functional style without structs would make more sense:

float calculate_whatever(int a, float b);

Or with the struct:

typedef struct {
  int a;
  float b;
} MyData;

int main() {
  MyData data={.a=1, .b=3.141f};

  float f=mydata_calculate(&data);
  // ...
}

Though if you plan on allocating many of those structs think about using SoA style and providing functions for looping over whole separate arrays.

Edited by wintertime
2

Share this post


Link to post
Share on other sites

@all - Thanks a lot, the information provided was very insightful to me, and I see that applying the "OO" style to my program is going to be overkill, adding unnecessary complexity.

 


Just makes me wish I was using C++ though - any reason you aren't?

Three reasons: Mainly because the program in question is simple, needs to be very close to the underlying hardware, and there actually isn't a compiler (that I know of) that is capable of compiling C++ code to the target architecture.

 

Even if there were a possibility to use C++, I'd neglect to do so. I feel more comfortable programming micro controllers in C because it keeps things clean and simple. Especially when others work with your code.

1

Share this post


Link to post
Share on other sites

So, I assume you're providing accessor functions (get_a(), get_b()) to ensure the data is only accessed by those functions, but the way you've implemented it, anyone can just access it via myData->a, etc.

 

If you want to hide the data, you can externally define the structure as an empty array in a header file you publish:

// myDataPublic.h
typedef struct
{
  unsigned char Data[8];
} MyData;
 
int MyDataGetA(MyData* data);
float MyDataGetB(MyData *data);

 

So whoever is using it doesn't know the contents of the struct.

 

Considering you're working on a small micro, processing/memory may be an issue, and trying to get fancy with accessors is a waste IMO.  Just allow them to access the members directly.

2

Share this post


Link to post
Share on other sites
I think the best way to implement data hiding in C is to have a unit provide handles and manage the memory for the actual data privately in the unit.

So you have like StringHandle getString(const char*) and FreeString(StringHandle) etc.

This is the only way I know to guarantee in C that data and implementation remain private and means you can centralize control of allocation.

But if you need thus kind of control you need a very good reason to be using C in the first place.
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0