473,969 Members | 44,025 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about C/C++ struct memory layout compatibility

Supposing you have the following declaration in a header file:
-- head.h --

struct s {
int c;
char v;
#if defined(__cplus plus)
s();
s(double);
method1(int);
method2(float);
#endif
};

and then two sources,

-- cppsrc.cc --
#include "head.h"

extern "C" {
s *create(void)
{
s *res = new s(1.0);
res->method1(4);
return res;
}

-- csrc.c --
#include "head.h"

extern struct s *create(void);

int f()
{
struct s* val = create();
val->v = 'd';
}

Is this legal/portable?

Thanks in advance,
Vincenzo
Jul 23 '05
13 1990


Vincezo Ciaschini wrote:
Because they would not be in the same program. The cppsrc.cc would be
in a dynamic library that will be linked to the program containing
csrc.c. It is an effort to provide a C interface to a C++ library.


You probably need to make a clean-cut between the C++ interface and the
C interface, instead of attempting to mix them both in a single header
file. Use 2 header files:

head.h
------
normal C++ declaration here

head_c.h
--------
C declarations here

hth,
-shez-

Jul 23 '05 #11
Victor Bazarov <v.********@com Acast.net> wrote in news:8oYpe.8196 5
$N********@news read1.mlpsca01. us.to.verio.net :
Vincezo Ciaschini wrote:
Supposing you have the following declaration in a header file:
-- head.h --

struct s {
int c;
char v;
#if defined(__cplus plus)
s();
s(double);
method1(int);
method2(float);
#endif
};

and then two sources,

-- cppsrc.cc --
#include "head.h"

extern "C" {
s *create(void)
{
s *res = new s(1.0);
res->method1(4);
return res;
}

-- csrc.c --
#include "head.h"

extern struct s *create(void);

int f()
{
struct s* val = create();
val->v = 'd';
}

Is this legal/portable?
I have no definite answer to this question. I believe, though, that
it would be implementation-specific. However, regardless of what I
believe, I'd like to know *why* would you want to do something like
that. Why do you feel the need to have both C and C++ translation

units in the same program?


Arguably wouldn't that violate the One Definition Rule ?
Jul 23 '05 #12
On 9 Jun 2005 08:37:17 -0700, Rapscallion wrote:
Vincezo Ciaschini wrote:
Supposing you have the following declaration in a header file:
-- head.h -- ...

Is this legal/portable?


Maybe. But the classic way to provide a C interface for a C++ program
goes like this:

// somefile.h ----------------------
struct s; // declaration

extern "C" {
struct s *create(void);
void destroy (struct s* x);
void method1 (struct s* x, int);
// ...
}
// somefile.cpp --------------------
struct s {
s();
s(double);
method1(int);
method2(float);
private:
int c;
char v;
};

s *create(void)
{
struct s *res = new s(1.0);


This new could throw an exception. You should use the one that returns a
NULL pointer on error and handle that case.
res->method1(4);
return res;
}

void destroy (struct s* x) { ... }
void method1 (struct s* x, int) { ... }
// the C user ------------------------
int main() {
struct s* val = create();
method1 (s, 33);
destroy (s);
}

--
I'm not a racist. I hate everyone equally!
Jul 23 '05 #13
Rene Moehring wrote:
On 9 Jun 2005 08:37:17 -0700, Rapscallion wrote:

....
s *create(void)
{
struct s *res = new s(1.0);


This new could throw an exception. You should use the one that returns a
NULL pointer on error and handle that case.
res->method1(4);
return res;
}


Good point. Not only new may throw but also 'res->method1(4)'. One
needs to surround function bodies with try {} catch (...){} blocks.

Jul 23 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
5846
by: Cgacc20 | last post by:
I have a c struct from old code that cannot be modified and I am trying to write a wrapper C++ class around it. This class is often passed as a pointer to some c functions of a library and I wanted to keep my new class transparent and compatible with those functions. That is, when using the code, I should be able to do either: oldVector xyz; function( &xyz ); myVector xyz; function( &xyz );
6
2210
by: Arthur J. O'Dwyer | last post by:
As far as I know, C89/C90 did not contain the now-standard offsetof() macro. Did C89 mandate that structs had to have a consistent layout? For example, consider the typical layout of the following structure: struct weird { int x; /* sizeof(int)==4 here */
5
2405
by: Kobu | last post by:
In embedded systems (programmed in C), often times structure declarations are used to group together several status/control/data registers of external hardware (or even internal registers). The example below (from GBD's THE C BOOK) uses this. Is this a portable method? . /*
2
12378
by: Al Bahr | last post by:
H I am try to convert C# to vb.net I don’t know what would be an equivalent statements in VB.net any help will be appreciated. bmiColors ' RGBQUAD structs... Blue-Green-Red-Reserved, repeat.. ' <summary ' Palette entry structur ' </summary bmiColors ' RGBQUAD structs... Blue-Green-Red-Reserved, repeat..
10
1319
by: Bonj | last post by:
I almost understand TSTs, to the point where I just need to know the answer to this: When making a TST (in C++) that will have as its leaf nodes words that make up SQL language and an categorising identifier for each one, and each layer of the tree will represent comparison of a further letter within the search string, what will happen when a particular node is a leaf node itself, but also has leaf nodes of its own? i.e. specifically, as...
7
1654
by: Jake Thompson | last post by:
Hello I have the following defined structure struct cm8linkstruc { char *type; /* type of item*/ char *desc; /* description of item */ char *item_increment; /* increment value for item in folder */
19
1525
by: lawtrevor | last post by:
I have a question regarding the use of free() based on some code I need to decipher: { struct A {<A fields>}; struct B {<A fields+ <Bfields>}; typedef struct A Aobj; typedef struct B Bobj;
28
3674
by: kyle york | last post by:
Greetings, Why does the C standard require the members of a structure not be re-ordered (6.2.5.20)? Padding is allowed, and platform dependent, which means one cannot rely on the exact layout anyway, so what's the point? Without this restriction the compiler could layout the structure in the most efficient way possible, for some definition of efficient. It would be easy enough to turn this reordering off with a compiler specific...
4
9827
by: hugo.arregui | last post by:
Hi! I have two struts like that: struct { int num; int num2; struct b arrayOfB; } a;
0
10135
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11792
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10048
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8427
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7578
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6377
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6514
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4700
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.