Funktionerna malloc() och calloc() är biblioteksfunktioner som allokerar minne dynamiskt. Dynamisk betyder att minnet tilldelas under körning (programkörning) från heapsegmentet.
Initialisering
malloc() allokerar ett minnesblock av given storlek (i byte) och returnerar en pekare till början av blocket. malloc() initierar inte det tilldelade minnet. Om du försöker läsa från det tilldelade minnet utan att först initiera det, kommer du att anropa odefinierat beteende, vilket vanligtvis betyder att de värden du läser kommer att vara skräpvärden.
java cast char till sträng
calloc() allokerar minnet och initierar även varje byte i det allokerade minnet till 0. Om du försöker läsa värdet på det allokerade minnet utan att initiera det, får du 0 eftersom det redan har initierats till 0 av calloc().
Parametrar
malloc() tar ett enda argument, vilket är antal byte att tilldela.
Till skillnad från malloc() tar calloc() två argument:
- Antal block som ska tilldelas.
- Storleken på varje block i byte.
Returvärde
Efter lyckad allokering i malloc() och calloc() returneras en pekare till minnesblocket annars returneras NULL vilket indikerar fel.
Exempel
C-koden nedan visar skillnaden mellan malloc- och calloc-funktioner för att allokera dynamiskt minne.
C
// C code that demonstrates the difference> // between calloc and malloc> #include> #include> int> main()> {> >// Both of these allocate the same number of bytes,> >// which is the amount of bytes that is required to> >// store 5 int values.> >// The memory allocated by calloc will be> >// zero-initialized, but the memory allocated with> >// malloc will be uninitialized so reading it would be> >// undefined behavior.> >int>* allocated_with_malloc =>malloc>(5 *>sizeof>(>int>));> >int>* allocated_with_calloc =>calloc>(5,>sizeof>(>int>));> >// As you can see, all of the values are initialized to> >// zero.> >printf>(>'Values of allocated_with_calloc: '>);> >for> (>size_t> i = 0; i <5; ++i) {> >printf>(>'%d '>, allocated_with_calloc[i]);> >}> >putchar>(>'
'>);> >// This malloc requests 1 terabyte of dynamic memory,> >// which is unavailable in this case, and so the> >// allocation fails and returns NULL.> >int>* failed_malloc =>malloc>(1000000000000);> >if> (failed_malloc == NULL) {> >printf>(>'The allocation failed, the value of '> >'failed_malloc is: %p'>,> >(>void>*)failed_malloc);> >}> >// Remember to always free dynamically allocated memory.> >free>(allocated_with_malloc);> >free>(allocated_with_calloc);> }> |
>
>
sträng en intProduktion
Values of allocated_with_calloc: 0 0 0 0 0 The allocation failed, the value of failed_malloc is: (nil)>
Skillnaden mellan malloc() och calloc() i C
Låt oss se skillnaderna i tabellform:
| Ja Nej. | malloc() | calloc() |
|---|---|---|
| 1. | malloc() är en funktion som skapar ett minnesblock med en fast storlek. | calloc() är en funktion som tilldelar ett specificerat antal minnesblock till en enda variabel. |
| 2. | malloc() tar bara ett argument | calloc() tar två argument. |
| 3. | malloc() är snabbare än calloc. | calloc() är långsammare än malloc() |
| 4. | malloc() har hög tidseffektivitet | calloc() har låg tidseffektivitet |
| 5. | malloc() används för att indikera minnesallokering | calloc() används för att indikera kontinuerlig minnesallokering |
| 6. | Syntax: void* malloc(storlek_t storlek); | Syntax : void* calloc(storlek_t antal, storlek_t storlek); |
| 8. | malloc() initierar inte minnet till noll | calloc() initierar minnet till noll |
| 9. | malloc() lägger inte till några extra minneskostnader | calloc() lägger till lite extra minneskostnader |