473,473 Members | 1,857 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

struct initializer efficiency and portability

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.

Aug 28 '07 #1
1 1367
Laurent Deniau <Laurent.Den...@gmail.comwrote:
// 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

Aug 28 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have...
2
by: Allan Bruce | last post by:
I have made a struct which has several fields and I initialise like this: MyStruct foo = {a, b, c, d, e}; I just added another field to the struct at the end, and I thought I would have to add...
5
by: Kobu | last post by:
In embedded systems (programmed in C), often times structure declarations are used to group together several status/control/data registers of external hardware (or even internal registers). The...
3
by: Karl M | last post by:
Hi everyone, I just notice some strange behaviors on the MS C++ compiler regarding struct default constructor, see the example bellow: struct MyStruct { int a; }; class MyClass { public:
4
by: Michael Brennan | last post by:
I have a menu_item structure containing an union. func is used if the menu item should use a callback, and submenu if a popupmen should be shown. struct menu_item { enum { function, popup }...
6
by: Daniel Rudy | last post by:
Hello Group. Please consider the following code: /* this table is used in the wipedevice routine */ static const struct wipe_t { uchar wte; /* wipe table entry */ } wipetable = {...
0
by: Laurent Deniau | last post by:
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; }; // could other...
3
by: Bob Altman | last post by:
Hi all, If I have a class that includes an instance of a struct as a member, how do I initialize that struct? I can't find a syntax for the constructor "initializer list" that works. For...
2
by: dj3vande | last post by:
Is this code, as the complete contents of a translation unit, valid C90 that initializes the struct foo to contain copies of the values passed to bar()? -------- struct foo { int i; void *v;...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.