C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

[Question] parameter passing between C and C++

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Seung-Uk Oh
Guest





PostPosted: Fri Jun 27, 2003 12:31 pm    Post subject: [Question] parameter passing between C and C++ Reply with quote



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
Back to top
Victor Bazarov
Guest





PostPosted: Fri Jun 27, 2003 1:44 pm    Post subject: Re: [Question] parameter passing between C and C++ Reply with quote



"Seung-Uk Oh" <suoh (AT) macroimpact (DOT) com> wrote...
Quote:
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++.

Quote:

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.

Quote:

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



Back to top
Alexander Terekhov
Guest





PostPosted: Fri Jun 27, 2003 2:14 pm    Post subject: Re: [Question] parameter passing between C and C++ Reply with quote




Victor Bazarov wrote:
Quote:

"Seung-Uk Oh" <suoh (AT) macroimpact (DOT) com> wrote...
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++.

Friday.

regards,
alexander.

Back to top
Jerry Coffin
Guest





PostPosted: Sat Jun 28, 2003 1:59 am    Post subject: Re: [Question] parameter passing between C and C++ Reply with quote

In article <vfoii2cp9ebd7e (AT) corp (DOT) supernews.com>, [email]v.Abazarov (AT) attAbi (DOT) com[/email]
says...
Quote:
"Seung-Uk Oh" <suoh (AT) macroimpact (DOT) com> wrote...
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++.

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.

[ ... ]

Quote:
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.

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.

Quote:
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.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.