[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.



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Morten Nissen wrote:

> 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 );

cirka det samme som

hejsa = strdup("Hejsa")
printf("%s", hejsa)
free(hejsa)

De facto måden at gøre det på tror jeg, hvis man har strenge af dynamisk
længde. Husk på her laver vi en mem allokering på heapen.

> Eks 2.
> char* hejsa2 = "Hejsa2";
> printf( hejsa2 );

streng med constant længde, altså fixed, kan ikke free's.

> Eks 3.

Samme

> 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.

Du skal kun bruge løsning 1 hvis du blot bruger strengen midlertidigt,
eller du på et givent tidspunkt har behov for dynamisk at allokere
memory. Somregl er størrelsen af input ikke fixed, wrt. strenge.

Formater o.l. er somregl constante strenge.

Mvh,
Bo
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFHTfBJR6QuoOIhQf4RAidIAJ49SVVPhR0FuKJkif59MtikvfXTkACghB9L
GVP9b2WTVaCeI1e2CctQaRE=
=lQbv
-----END PGP SIGNATURE-----


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2007-12-01, 02:01 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] *