[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: [PROGRAMMERING] c malloc osv.



Morten Nissen wrote:

> Hejsa Liste,
> 
> Igen, jeg er ny i c, men har fra tid til anden lavet noget c++.
> 
> Jeg er kommet noget i tvivl om hvordan det er med c og hukommelses
> håndtering, og hvornår det er nødvendigt at bruge malloc/free osv.
> 
> Eks 1.
> char* hejsa = (char *) malloc( 6 );
> strcpy( hejsa, "Hejsa" );
> printf(hejsa);
> free( hejsa );
> 
> Eks 2.
> char* hejsa2 = "Hejsa2";
> printf( hejsa2 );
> 
> Eks 3.
> char hejsa3[7] = "Hejsa3";
> printf(hejsa3);
> 
> 
> Hvad er den store forskel her? Bort set fra at eks2 kun har 50% så mange
> linjer, plus jeg ikke har sagt hvor stort et område jeg skal bruge.
> Eks 3 er lidt åbenlys at den skiller sig ud, men jeg er meget i tvivl om
> hvad der er best pracsis her.

Kort svar: Eks. 3 og dog...

Konstante tekst strenge skal defineres som array. Der burde i dit eksempel
stå: 
   const char hejsa3[] = "Hejsa3";

Hvis formålet "kun" er at udskrive teksten. Hvis der derimod skal ændres på
indholdet af teksten skal definitionen være:
   
   char hejsa3[] = "Hejsa3";

Og hvorfor så det... ja, nu vil jeg ikke skrive 500Kb stor mail. Men,
derimod referer til: 

   Ulrich Drepper, Red Hat, Inc.
   How To Write Shared Libraries
   http://people.redhat.com/drepper/dsohowto.pdf

Super god læsning. Læs specielt afsnit: 2.4 Choosing the Right Type


Håber det hjalp dig i den rigtige retning.

(John =;-)



 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2008-01-01, 17:38 CET [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] *