malloc() Function in C library with EXAMPLE - Guru99
文章推薦指數: 80 %
The malloc() function stands for memory allocation. It is a function which is used to allocate a block of memory dynamically.
Skiptocontent
WhatismallocinC?
Themalloc()functionstandsformemoryallocation.Itisafunctionwhichisusedtoallocateablockofmemorydynamically.Itreservesmemoryspaceofspecifiedsizeandreturnsthenullpointerpointingtothememorylocation.Thepointerreturnedisusuallyoftypevoid.Itmeansthatwecanassignmallocfunctiontoanypointer.
Syntax
ptr=(cast_type*)malloc(byte_size);
Here,
ptrisapointerofcast_type.
Themallocfunctionreturnsapointertotheallocatedmemoryofbyte_size.
Example:ptr=(int*)malloc(50)
Whenthisstatementissuccessfullyexecuted,amemoryspaceof50bytesisreserved.Theaddressofthefirstbyteofreservedspaceisassignedtothepointerptroftypeint.
Consideranotherexampleofmallocimplementation:
#include
延伸文章資訊
- 1How to use "malloc" in C - Educative.io
Memory allocation (malloc), is an in-built function in C. This function is used to assign a speci...
- 2Dynamic Memory Allocation in C using malloc(), calloc(), free ...
C malloc() method ... The “malloc” or “memory allocation” method in C is used to dynamically allo...
- 3C library function - malloc() - Tutorialspoint
C library function - malloc(), The C library function void *malloc(size_t size) allocates the req...
- 4malloc - cppreference.com
- 5malloc() Function in C library with EXAMPLE - Guru99
The malloc() function stands for memory allocation. It is a function which is used to allocate a ...