Re: A concern about mixing C and C++



Ian Collins wrote:
Ark wrote:
Ian Collins wrote:

Ark wrote:

Ian Collins wrote:

C++ gives the developer a choice between the C way of doing something
and the C++ way.

Ehmmm... please help me here: I am not a C++ guy.
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 );
}

1. Tentative declarations of variables are (in C) indistinguishable from
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.

1. It is not an easy fix if I want x, y, z to also be static. (Actually, regardless of whether they are const).
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
.



Relevant Pages

  • Re: A concern about mixing C and C++
    ... C++ does not allow all the C way (example: tentative declarations). ... doesn't know e.g. about any exceptions that may be thrown in a function ... 'struct' in a declaration, that is: ... const struct X *next} X; ...
    (comp.lang.c)
  • Re: A concern about mixing C and C++
    ... doesn't know e.g. about any exceptions that may be thrown in a function ... If my understanding of tentative declarations is correct, ... 'struct' in a declaration, that is: ... const struct X *next} X; ...
    (comp.lang.c)
  • Re: Why "const rect& rhs" is used here?
    ... declared as const, as far as i can see, i also ... char pcStudentFirstName; ... // Example where struct is copied. ... void SetStudentName ...
    (microsoft.public.vc.language)
  • [PATCH 1/4] TOMOYO: Add caller tasks credential condition support.
    ... * Returns true on success, ... struct tomoyo_domain_info *domain, ... const bool is_delete) ...
    (Linux-Kernel)
  • Re: [PATCH][RFC] security: constify seq_operations
    ... A while ago I made a semantic patch to introduce const on file_operations ... struct file_operations x; ... .llseek = seq_lseek, ... .owner = THIS_MODULE, ...
    (Linux-Kernel)