Re: improve that function (bytes_dig_int)

Liste des GroupesRevenir à cl c  
Sujet : Re: improve that function (bytes_dig_int)
De : fir (at) *nospam* grunge.pl (fir)
Groupes : comp.lang.c
Date : 12. Apr 2024, 16:22:41
Autres entêtes
Organisation : i2pn2 (i2pn.org)
Message-ID : <uvbg3i$sm6s$1@i2pn2.org>
References : 1 2
User-Agent : Mozilla/5.0 (Windows NT 5.1; rv:27.0) Gecko/20100101 Firefox/27.0 SeaMonkey/2.24
Tim Rentsch wrote:
fir <fir@grunge.pl> writes:
>
it seems that this group likes such kind of topics, so
here you got
>
improve that function - i wrote it in quick and feel tired so i
dont want to improve it as for now
>
i got "list" of bytes (by list i mean pointer to ram area of
elements - loke reallocked by reallock, by aray i would rather
aybe name fully static array and heap seeds are more like lists
especially if you use them as lists - and got methods like "add"
(remove etc)
>
   unsigned char* bytes = NULL;
   int bytes_size = 0;
   int bytes_allocked = 0;
   int bytes_cursor = 0;
>
i wint to write a function DigInt that "digs" one int form that
list and returns its, then the "cursor" is set after that and you
can call it again untill all ints are "digged"
>
[code]
>
I usually prefer (and recommend) writing code that doesn't make
use of global state, as for example:
>
long
next_value( char *s0, char **next_s ){
   char *s = s0 + strcspn( s0, "0123456789" );
   return
     !*s
       ?   *next_s = s0,  0
       :   strtol(  s - (s!=s0 && s[-1]=='-'),  next_s,  10  );
}
>
Retrieving and processing successive values can be done using
this function by means of a simple for() loop, as illustrated by
the code below:
>
void
print_longs( char *s ){
   long v;
>
   printf( " input:  %s\n", s );
>
   for(  char *next;  v = next_value( s, &next ), s != next;  s = next  ){
     printf( "   value:  %12ld\n", v );
   }
>
   printf( "\n" );
}
>
Note that the local variable 'next' here takes the place of a
cursor for the next input.
>
well ok..this is kinda tricky style but thats a matter of personal style
i prefer more "strightforward" long descriptive names etc
two remarks here
1) people shoudl avoid imo talking a word "global" becouse in
normal desctop computing at least those variables are not global but
typically belong to library (dll) so they are library scope/module
scope not global (this is my own remark as i noticed this) (thay may also be exe scope but thsi is also module scope
so global dont much exist, though there is this problem that when same
symbol is uset in various libraries it may cause serious conflict
2) as to avaidingf global state i also thing would not agree
if the satae is "meaningfull" - like if variable name is really meanigfull for given module - it could be used (i mean it should
be as meaningful as functions provided, than can be "global" (module scoe) too imo by analogy
various "global" (id est module scope)  arrays for exampel are quite meaningfull so no problem them being module scope (and dont listen
to c++ ponys what they say imo, fuck them - in short - i would say ;c)

Date Sujet#  Auteur
3 Apr 24 * improve that function (bytes_dig_int)6fir
3 Apr 24 +* Re: improve that function (bytes_dig_int)2fir
3 Apr 24 i`- Re: improve that function (bytes_dig_int)1fir
9 Apr 24 `* Re: improve that function (bytes_dig_int)3Tim Rentsch
12 Apr 24  `* Re: improve that function (bytes_dig_int)2fir
13 Apr 24   `- Re: improve that function (bytes_dig_int)1Tim Rentsch

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal