Re: A concern about mixing C and C++
- From: Ark <akhasin@xxxxxxxxxxxxxxxxxxxx>
- Date: Sun, 30 Jul 2006 13:09:58 -0400
Ian Collins wrote:
Ark wrote:1. It is not an easy fix if I want x, y, z to also be static. (Actually, regardless of whether they are const).Ian Collins wrote:
Ark wrote:1. Tentative declarations of variables are (in C) indistinguishable from
Ian Collins wrote:
C++ gives the developer a choice between the C way of doing somethingEhmmm... please help me here: I am not a C++ guy.
and the C++ way.
AFAIK, C++ does not allow all the C way (example: tentative
declarations).
Then, if I write C-style in C++, at the very minimum the C++ compiler
doesn't know e.g. about any exceptions that may be thrown in a function
I call and which is in a different translation unit. So it just has to
bring in the heavy machinery in just in case. Am I missing something?
If my understanding of tentative declarations is correct, they are
synonymous with forward declarations in C++ due to the differing use of
'struct' in a declaration, that is:
struct X;
forward declares the type X.
The compiler doesn't have to bother with unexpected exceptions when
compiling idiomatic C code, any unhandled exception can be caught by
whatever calls main().
As a trivial example, on my platform the following generated essentially
the same code when compiled as C or C++:
extern int fn(void);
extern void fn1(int);
typedef struct { int n; } X;
int main(void)
{
X x;
x.n = fn();
fn1( x.n );
}
definitions until the end of the translation unit; they AFAIK expressly
prohibited in C++. E.g., a (const) circular data structure like
typedef struct X {int x, const struct X *next} X;
typedef struct X {int x; const struct X *next;} X;
Is valid C but not C++.
typedef struct X {int x; const X *next;} X;
Is valid C++ but not C.
typedef struct X_t {int x; const struct X_t *next;} X;
Is valid C++ and C.
const X x;
const X y;
const X z = {5, &x};
const X y = {6, &z};
const X x = {7, &y};
is a valid C but not C++.
Correct , C++ prohibits uninitialised consts. Easily fixed for both thus:
extern const X x;
extern const X y;
const X z = {5, &x};
const X y = {6, &z};
const X x = {7, &y};
2. Interesting example; just curious what the differences are and
whether it matters that your function is called main, not, say, foo.
The C++ compiler pushed one extra register on the stack. The function
name does not make any difference.
2. It's interesting to understand /why/ C++ uses more stack. Is it, by any chance, for passing (e.g. passing through) a pointer to an exception object? In any event, those things get compounded... definitely a consideration for resource-constrained environments...
- Ark
.
- References:
- A concern about mixing C and C++
- From: ziman137
- Re: A concern about mixing C and C++
- From: Malcolm
- Re: A concern about mixing C and C++
- From: Ian Collins
- Re: A concern about mixing C and C++
- From: Malcolm
- Re: A concern about mixing C and C++
- From: Ian Collins
- Re: A concern about mixing C and C++
- From: Ark
- Re: A concern about mixing C and C++
- From: Ian Collins
- Re: A concern about mixing C and C++
- From: Ark
- Re: A concern about mixing C and C++
- From: Ian Collins
- A concern about mixing C and C++
- Prev by Date: Re: Pointer to pointer with lots of examples
- Next by Date: Re: A concern about mixing C and C++
- Previous by thread: Re: A concern about mixing C and C++
- Next by thread: Re: A concern about mixing C and C++
- Index(es):
Relevant Pages
|