Re: macro for fir list?

Liste des GroupesRevenir à l c 
Sujet : Re: macro for fir list?
De : fir (at) *nospam* grunge.pl (fir)
Groupes : comp.lang.c
Date : 30. Mar 2024, 11:56:14
Autres entêtes
Organisation : i2pn2 (i2pn.org)
Message-ID : <uu8nju$3gvnj$1@i2pn2.org>
References : 1 2 3 4 5
User-Agent : Mozilla/5.0 (Windows NT 5.1; rv:27.0) Gecko/20100101 Firefox/27.0 SeaMonkey/2.24
fir wrote:
fir wrote:
fir wrote:
Opus wrote:
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.
>
>
i think teh idea is rather not obvius, i agree people use dynamic
alocation, more rarely they use reallock atc but the pure idea
of list like here is imo not much in use imo... - if that would be in
use it would be generally used as this is some kind of composition
pattern and that would substitule ways people usually du such things
in c largely...as to the line you say i may test it
>
i coode in c++ mode and in c mode tu though in c++ more often
>
checked seem not to work: error: taking adress of temporary
>
as this list idea people may obviously take it uuse it and then say, oh,
i was using it - but in facy as i know how c people code (right from
this group etc) they dont use it, unless thay maybe begin using it in
last few years
>
when i started using it (see my more historic post about 'chunks' and
'splitter') i used the appproach with variable holding the preallocated
value... then i dropped it becouse reallock already stores such number
under the hood so this is kinde reapeating the same thing - but then
later it showed that stiill doubling it may speed things becouse
(as far as i remember becouse i dont remember exact values ) this
call to reallock even if this do almosc nothing still may cost if this
is in more time critical case
>
>
>
>
note i presented somewhat complicated versions of it (for structures,
longer names and indeksed a key (which is in fact dictionary)
>
quite useble would be tiny crafted versions of it - most simple
>
>
// "ints.h" - int list cntainer
>
   int* ints = NULL; int ints_size = 0;
   void ints_add(int val)  {
      ints = (int*)realloc(ints, ++ints_size*sizeof(int));
      ints[ints_size-1] = val;
      return ;  }
>
>
(btw i consider such containers to be a part of my sickle.c library )
>
one could also write a one line add version, i hope its right
>
   int* ints = NULL; int ints_size = 0;
>
   void ints_add(int
val){(ints=(int*)realloc(ints,++ints_size*sizeof(int)))[ints_size-1]=val;}
>
if that would be so common people wouldnt talk about crap c++ vector so
much
>
>
//main.c
>
  void Test()
   {
>
>
     ints_add(100);
     ints_add(101);
     ints_add(102);
     ints_add(114);
>
     for(int i=0; i<ints_size;i++) printf("\n%d", ints[i]);
>
     ints_size=0; //reset
>
     ints_add(200);
     ints_add(221);
     ints_add(202);
     ints_add(214);
>
     for(int i=0; i<ints_size;i++) printf("\n%d", ints[i]);
>
  }
>
im not quite belive people use it as its such good idea if people would
use it it should be regularelly known and used and i never seen nor hear
of this (at least not before i get to use it few years ago)
>
people rather use c__ vectror sh*t instead of that and that is better
>
programming is a lot about composition and this changes composition
a lot
>
>
yet other example
//bytes container
   char* bytes = NULL; int bytes_size = 0;
   void bytes_add(char val) { (bytes=(char*)realloc(bytes,++bytes_size*sizeof(char)))[bytes_size-1]=val;   }
   void bytes_load(char* name)  {    FILE *f = fopen(name, "rb"); int c; while((c=getc(f))!=EOF) bytes_add(c);   fclose(f);  }
   void bytes_save(char* name)  {    FILE* f =fopen(name, "wb"); fwrite (bytes , 1, bytes_size, f); fclose (f);  }
this piece of code has generally quite big usega of being ablo to quick
work on files, like for example
   void Test()
   {
   bytes_load("text.txt");
   for(int i=0; i<bytes_size; i++)
    if(bytes[i]>='a'&bytes[i]<='z')
      bytes[i]+='A'-'a';
   bytes_save("text_uppercased.txt");
}

Date Sujet#  Auteur
28 Mar 24 * macro for fir list?20fir
28 Mar 24 +* Re: macro for fir list?4fir
28 Mar 24 i`* Re: macro for fir list?3fir
28 Mar 24 i `* Re: macro for fir list?2fir
29 Mar 24 i  `- Re: macro for fir list?1fir
30 Mar 24 `* Re: macro for fir list?15Opus
30 Mar 24  `* Re: macro for fir list?14fir
30 Mar 24   +- Re: macro for fir list?1fir
30 Mar 24   `* Re: macro for fir list?12fir
30 Mar 24    `* Re: macro for fir list?11fir
30 Mar 24     +* Re: macro for fir list?9fir
30 Mar 24     i`* Re: macro for fir list?8bart
30 Mar 24     i +* Re: macro for fir list?6fir
30 Mar 24     i i`* Re: macro for fir list?5fir
30 Mar 24     i i `* Re: macro for fir list?4fir
30 Mar 24     i i  +- Re: macro for fir list?1fir
30 Mar 24     i i  +- Re: macro for fir list?1fir
30 Mar 24     i i  `- Re: macro for fir list?1fir
30 Mar 24     i `- Re: macro for fir list?1fir
30 Mar 24     `- Re: macro for fir list?1fir

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal