[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [CPROG] Dynamisk alloceret arrays



Thomas M. wrote:
> On Sat, 7 Aug 1999, Sune Fischer wrote:
> > --- Per Vognsen <sslug@sslug> wrote:
> > > Thomas M. wrote:
> > > [snip]
> > > >  Er der en mulighed for at lave dynamisk
> > > allocerede arrays, således at de
> > > > kan vokse, når der bliver brug for det?
> >
> > Det kan lade sig gøre som omtalt i de andre indlæg, men det er min
> > erfaring at den slags koster tid. Hvis det er noget der skal gøres
> > meget ofte, fx inde i en loop, vil jeg kraftigt anbefale at du finder
> > en anden løsning. Eksempelvis prøver at estimere den maksimale
> > størrelse på arrayet funktionen får brug for, og malloc'er i
> > begyndelsen (og free'er i slutningen!).
> > Det er bedre at spilde en MB og så få et program der kører 10 fold
> > hurtigere efter min mening.
> 
>  Hastigheden har ikke noget at sige, da den alligevel skal vente på bruger
> input hele tiden. Desuden er det i 'klumper' de nye data kommer til, altså
> noget lign. :

   Selv om hastigheden kan vaerre et problem, er det (normalt) daarlig
programmeringspraksis at bygge dit system paa arbitraere tal. Med et
system hvor en slags buffer vokser efter behov vil du aldrig faa de
problemmer. Ellers er det bare den gamle memory vs. speed debat om igen.

>  genre="rock"
>  <vente på bruger skal browse lidt i nogle menuer>
>  and artist="Metallica"
>  <vente på bruger skal browse i lidt flere menuer>
>  and album="Load"

   Som der blev skrevet i en anden besked saa er det nok sagen men en
String class hvis du bruger C++. Det goer det meget let at faa din
buffer til at vokse. Det kan dog (selvfoelgelig) ogsaa lade sig goere i
vanilla C, hvis du indkapsler din char pointer i en struktur.
Pseudo-agtig kode for nogle funktioner:

   function set(String string, char *s)

   var len : integer;

   begin
      if ((len = strlen(s)) > string.currentLength))
         grow(string, len);

      string.s = s;
   end

   function grow(String string, int len)

   begin
      if (string.currentLength < len)
         string.alloc(len+delta);
   end

> Thomas Mørch
> licensed ham, with callsign OZ2AFO. License category: CEPT-1

-- 
| Per Vognsen (Randers - Denmark)| "Superstition is a  word the |
| Mail: <sslug@sslug>   | ignorant  use   to  describe |
| Voice: +45 20639009            | their own ignorance." - Sifu |


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 20:07 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *