struct initializer efficiency and portability

L

Laurent Deniau

I was wondering if there is any good reason (semantic) to assume that
the following three code do not have the same efficiency (both space
and time):

struct A { int a,b,c; }; // example could take different types with
less/more fields.
extern void g(const struct A*);

// 1st case, C99
void f1(int a, int b, int c) {
g(&(struct A*){ a,b,c }); // compound litteral
}

// 2nd case, C99
void f2(int a, int b, int c) {
struct A a = { a,b,c }; // non-constant struct initializer
g(&a);
}

// 3rd case, C89
void f3(int a, int b, int c) {
struct A a;
a.a = a, a.b = b, a.c = c; // field assignment
g(&a);
}

gcc generates the same code for the three cases. Would it be naive to
assume this behavior from a C compiler?

a+, ld.
 
L

Laurent Deniau

Laurent Deniau said:
// 1st case, C99
void f1(int a, int b, int c) {
g(&(struct A*){ a,b,c }); // compound litteral

Oups, should be:
g(&(struct A){ a,b,c }); // compound literal
 

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,754
Messages
2,569,527
Members
44,999
Latest member
MakersCBDGummiesReview

Latest Threads

Top