[Question] parameter passing between C and C++

S

Seung-Uk Oh

Hi, everyone!

We are developing an application using both C and C++.
We have defined a structure in a C program as follows:

typedef struct node {
struct node *next;
int value;
}List;

And we construct a linked list using the above structure.
Then we pass the linked list to a function which is defined
in a C++ program enclosed with "extern "C" { }".

e.g.)
IN C-program

typedef struct node {
struct node *next;
int value;
}List;

int main(){
List *l;

/* l is initialized */

function_in_cpp(l);
}

IN C++-program

extern "C" {
typedef struct node {
struct node *next;
int value;
}List;

function_in_cpp(List *l)
{
....
}
}

When the length of a linked list is 1, the function call to
a function in a C++ is successful. However, when we call the function
in C++ program with the linked list whose length is 2, the actual
parameter in the C++ program is NULL. So, we can't pass the desired
value to a function in the C++ program.

Is there any difference between C and C++ in representing a "struct"
type variable? If any, the difference results in the consequence?

Please anybody help me.

Thanks in advance
 
V

Victor Bazarov

Seung-Uk Oh said:
We are developing an application using both C and C++.
We have defined a structure in a C program as follows:

typedef struct node {
struct node *next;
int value;
}List;

And we construct a linked list using the above structure.
Then we pass the linked list to a function which is defined
in a C++ program enclosed with "extern "C" { }".

That may not work. C object model differs from C++ object
model. Objects created in C may not be recognised in C++.
e.g.)
IN C-program

typedef struct node {
struct node *next;
int value;
}List;

int main(){
List *l;

/* l is initialized */

function_in_cpp(l);
}

IN C++-program

extern "C" {
typedef struct node {
struct node *next;
int value;
}List;

function_in_cpp(List *l)
{
...
}
}

When the length of a linked list is 1, the function call to
a function in a C++ is successful. However, when we call the function
in C++ program with the linked list whose length is 2, the actual
parameter in the C++ program is NULL. So, we can't pass the desired
value to a function in the C++ program.

To verify that claim we would need a complete program. There is
no guarantee that at the moment of the call to the C++ function
with a list of length 2 you haven't make a mistake and the list
isn't really garbage.
Is there any difference between C and C++ in representing a "struct"
type variable? If any, the difference results in the consequence?

There probably is.

Objects created in the part of the program written in a certain
language should be processed in the part of the program written
in the same language, unless there is a way to convert them (like
XML document, for example, but that's very similar to basic I/O)
or they are passed by value interpreted the same way in both
languages (e.g. 'int' or 'char' or pointers thereof).

Victor
 
A

Alexander Terekhov

Victor said:
That may not work. C object model differs from C++ object
model. Objects created in C may not be recognised in C++.

Friday.

regards,
alexander.
 
J

Jerry Coffin

That may not work. C object model differs from C++ object
model. Objects created in C may not be recognised in C++.

Hmm...while it's difficult for the C++ standard to require it directly,
the struct above is pretty clearly a POD struct, and it does its best to
ensure that POD structs will be layout compatible with C.

[ ... ]
There probably is.

There shouldn't normally be. As I said above, it's virtually impossible
for the C++ standard to come out and directly say it has to be
compatible with C, it comes about as close as the committee figured they
could to doing exactly that.
Objects created in the part of the program written in a certain
language should be processed in the part of the program written
in the same language, unless there is a way to convert them (like
XML document, for example, but that's very similar to basic I/O)
or they are passed by value interpreted the same way in both
languages (e.g. 'int' or 'char' or pointers thereof).

This can certainly simplify things considerably. Perhaps more to the
point, there's not often much need to mix C and C++ for the simple
reason that at least if you have well-written source code for the C
part, chances are that converting it to compile as C++ will be fairly
trivial. Nonetheless, you don't always have well-written source code to
work with, and in that case, mixing C and C++ is perfectly reasonable
and passing structs back and forth between the two normally works quite
well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top