Deutsch   English   Français   Italiano  
<uu88uo$qv85$3@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Opus <ifonly@youknew.org>
Newsgroups: comp.lang.c
Subject: Re: macro for fir list?
Date: Sat, 30 Mar 2024 06:46:00 +0100
Organization: A noiseless patient Spider
Lines: 46
Message-ID: <uu88uo$qv85$3@dont-email.me>
References: <uu3s0m$3av2s$1@i2pn2.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 30 Mar 2024 05:46:00 +0100 (CET)
Injection-Info: dont-email.me; posting-host="06d95bffe698016a7647970c3e32c003";
	logging-data="883973"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/Nx1s725JDc8Ys8Q66LjNC"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:bfKzTzJZnHubz7y7AWhdIMcvVLo=
Content-Language: en-US
In-Reply-To: <uu3s0m$3av2s$1@i2pn2.org>
Bytes: 2953

On 28/03/2024 14:40, fir wrote:
> 
> you know famous fir list
> 
> fir list its a list container i somewhat "invented",
> i write "invented" as its reasonably simple thing but i never heard
> anyone uses it so as for me i invented it for personal use and use it

It's a pretty common thing though. It's just that it's not standard, so 
everyone does it pretty much in their own way.

In my way, I avoid to realloc the 'dynamic array' everytime a new item 
is appended, as it can be a costly operation. I pre-allocate for a 
certain number of items, and keep track of the current number of items 
and the current size of the 'dynamic array'. If there is enough room 
left, I just add the item, no realloc needed. If there isn't, then I 
realloc, with not just room for 1 item, but a whole new block, for the 
same reason. You get the idea.

This is a common way of dealing with such dynamic containers. (See: bump 
allocators, and all that...)

> some could usually wrote a more convenient Add "method" not to pass 
> structures but just arguments and struct assigment do inside

 From what I get - it's not fully clear - you'd like to directly pass a 
structure 'literal' to your function, rather than having to go via an 
intermediate local variable.

This is exactly what C99 has brought, among other things, for over 20 
years. Do not hesitate to use it: compound literals.

For instance with your code, you can do this instead:

Fir_List_AddOne(&(struct Fir_List_Entry){ 11, 22 });

Yes, the 'cast' in front of the braces is mandatory for compound literals.

Note that your posted code is incorrect, you need to either refer to 
'Fir_List_Entry' with 'struct Fir_List_Entry', or typedef it prior.
I think omitting the 'struct' is allowed in C++, not so in C.

Conversely, I'm not sure compound literals (which I find very handy) are 
available in C++. But if you were using C++, you wouldn't need to do the 
above anyway, so. Just mentioning it.