关于C / C ++ struct内存布局兼容性的问题 [英] Question about C/C++ struct memory layout compatibility

查看:52
本文介绍了关于C / C ++ struct内存布局兼容性的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您在头文件中有以下声明:

- head.h -


struct s {

int c;

char v;

#if定义(__ cplusplus)

s();

s( double);

method1(int);

method2(float);

#endif

};


然后是两个来源,


- cppsrc.cc -

#include" head.h" ;


externC {

s * create(void)

{

s * res = new s(1.0);

res- > method1(4);

返回res;

}


- csrc.c -

#include" head.h"


extern struct s * create(void);


int f()

{

struct s * val = create();

val-> v =''d'';

}


这是合法/便携吗?


提前致谢,

Vincenzo

解决方案

Vincezo Ciaschini写道:

假设您在头文件中有以下声明:
- head.h -

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

然后是两个来源,

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

extern" C" {
s * create(void)
{res /> s * res = new s(1.0);
res-> method1(4);
返回res;
}
- csrc.c -
#include" head.h"

extern struct s * create(void);

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

这是合法/便携吗?




我对这个问题没有明确的答案。不过,我相信它会是特定于实现的。但是,无论我相信什么,我都想知道*为什么*你想要做一些像

这样的事情。为什么你觉得需要在同一个程序中同时拥有C和C ++翻译单元




V


Vincezo Ciaschini写道:

假设您在头文件中有以下声明:
- head.h -

struct s {
int c;
char v;
#if defined(__ cplusplus)
s();
s(double);
method1(int);
方法2(浮动);
#endif
};

然后是两个来源,

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

extern" C" {
s * create(void)
{res /> s * res = new s(1.0);
res-> method1(4);
返回res;
}
- csrc.c -
#include" head.h"

extern struct s * create(void);

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

这是合法/便携吗?



我不这么认为。我认为它违反了一个定义规则。但是

我不确定在混合C和C ++时这是否适用。


-shez-


Victor Bazarov写道:

Vincezo Ciaschini写道:

假设你在头文件中有以下声明:
- head.h -

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

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

extern" C" {
s * create(void)
{res /> s * res = new s(1.0);
res-> method1(4);
返回res;
}
- csrc.c -
#include" head.h"

extern struct s * create(void);

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

这是合法/便携吗?

我对这个问题没有明确的答案。但我相信,它将是特定于实现的。但是,无论我相信什么,我都想知道*为什么*你想要做什么呢?为什么你觉得需要在同一个程序中同时拥有C和C ++翻译单元?



因为它们不在同一个程序中。动态库中的cppsrc.cc将是

,它将链接到包含

csrc.c的程序。这是为C ++库提供C接口的努力。


再见,

Vincenzo

V



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

struct s {
int c;
char v;
#if defined(__cplusplus)
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

解决方案

Vincezo Ciaschini wrote:

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

struct s {
int c;
char v;
#if defined(__cplusplus)
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?

V


Vincezo Ciaschini wrote:

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

struct s {
int c;
char v;
#if defined(__cplusplus)
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 don''t think so. I believe it violates the One Definition Rule. But
I''m not sure if this applies when mixing C and C++.

-shez-


Victor Bazarov wrote:

Vincezo Ciaschini wrote:

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

struct s {
int c;
char v;
#if defined(__cplusplus)
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?


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.

Bye,
Vincenzo

V



这篇关于关于C / C ++ struct内存布局兼容性的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆