5

I have a C++ struct with methods inside:

struct S
{
   int a;
   int b;

   void foo(void)
   {
       ... 
   };
}

I have a userprogram, written in C. Is it possible to get a pointer to a S-struct and access the member aand b?

2
  • 1
    yes, but only to a and b. I don't need access to foo. Commented Oct 21, 2012 at 17:38
  • The S-pointer points a C++ object. I want to access to this memory from C. Commented Oct 21, 2012 at 17:41

3 Answers 3

10

You can access the members of a struct written in C++ from a C-program given you ensure that the C++ additions to the struct syntax are removed:

// header
struct S {
  int a, b;

#ifdef __cplusplus
  void foo(); 
#endif
};

// c-file:
#include "header.h"

void something(struct S* s)
{
  printf("%d, %d", s->a, s->b);
}

The memory layout of structs and classes in C++ is compatible with C for the C-parts of the language. As soon as you add a vtable (by adding virtual functions) to your struct it will no longer be compatible and you must use some other technique.

Sign up to request clarification or add additional context in comments.

4 Comments

I guess it is worth mentioning that the trick will stop working as soon as you add a virtual table into the picture. Or start using inheritance...
vtables or just private part. IMO, that's the kind of solution which will start by working and then stop at the most inconvenient moment.
Having members with different access specifiers (private/public/protected) may also change layout of the struct.
What you want is a standard layout class. The requirements are defined in §9/7. For example inheritance is allowed under certain strict limitations.
3

If these methods are not virtual then it is OK. You can even have common header for C/C++ with using of __cplusplus macro:

struct S
{
   int a;
   int b;

#ifdef __cplusplus
   void foo(void)
   {
       ... 
   }
#endif /* end section for C++ only */
};

Remember that name of this struct in C is struct S not just S.

1 Comment

Upvoted for "Remember that name of this struct in C is struct S not just S."
2

How do you get an S if your program is written in C? My guess is that a most precise description is that your program is written in a mix of C and C++ and you want to access some members of a C++ struct in the C part.

Solution 1: modify your C part so that it is in the common subset of C and C++, compile the result as C++ and now you may gradually use whatever C++ feature you want. That's what lot of projects did in the past. The most well know recent one being GCC.

Solution 2: provide an extern "C" interface to S and use it in your C part.

#ifdef __cplusplus
extern "C" {
#endif
    struct S;
    int getA(S*);
    int getB(S*);
#ifdef __cplusplus
}
#endif

The part which provides the implementation of getA and getB must be compiled as C++, but will be callable from C.

6 Comments

This is a more correct way than the rest. C++ will do some name-mangling to struct's name. This will make it inaccessible IIRC
Well, the problem is more complicated. It's OS-programming. The kernel is implemented in C++. My userspace has only C runtime. I have some shared structs between file system and userspace, but these include C++ specific code.
@Razer, The fact that the C++ part comes from the OS doesn't change anything. You may have to maintain your extern "C" wrappers independently from the provider of the C++ interface but that could be the case as well in other circumstances. Or do I miss something in your setup?
To describe in more precisely: Let's take opendir: linux.die.net/man/3/opendir This returns a DIR* and exactly this DIR is implemented in C++ in the filesystem.
But, what about this thought: DIR* doesn't need to be accessed directly in C. Therefore I can define DIR* as simple unsigned int pointer. For simpler structs, with no virtual methods, the solution from @mauve works.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.