Alternative of Malloc in C - GeeksforGeeks

28 Jan, 2022
An array in C or C++ is a collection of items stored at contiguous memory locations and elements can be accessed randomly using indices of an array. They are used for storing similar types of elements as the data type must be the same for all elements. They can be used to store the collection of primitive data types such as int, float, double, char, etc of any particular type. To add to it, an array in C or C++ can store derived data types such as the structures, pointers, etc. Given below is the picturesque representation of an array.
There are two types of array/ string declaration in C++ as listed as follows
Static DeclarationDynamic Declaration
Static Declaration
Dynamic Declaration
Way 1: Static Declaration
Methods: The static declaration can be done in 3 ways
Array declaration by specifying the size
Array declaration by initializing the elements
Array declaration by specifying the size and initializing elements
Array declaration by specifying the size
Syntax:
int arr1[10];
// Array declaration by specifying size
int n = 10;
int arr2[n];
Note: With recent C/C++ versions, we can also declare an array of user-specified size
Array declaration by initializing the elements
int arr[] = {10, 20, 30, 40};
// Array declaration by initializing elements
Compiler creates an array of size 4. This is same as “int arr[4] = {10, 20, 30, 40}”
Array declaration by specifying the size and initializing elements
Syntax:
int arr[6] = {10, 20, 30, 40} ;
// Array declaration by specifying size
// and initializing elements
Compiler creates an array of size 6, initializes first 4 elements as specified by user and rest two elements as above is same as follows:
int arr[] = {10, 20, 30, 40, 0, 0}"
Note: In a static declaration, the memory will get allocated in the stack memory.
Implementation: Below is the C program to demonstrate static declaration
Example 1:
C
// C Program to Implement Static Declaration // Importing standard input output operations file#include <stdio.h> // Main driver methodint main(){  // Static declaration of array so it  // will get memory in Stack memory  int first_array[2];     // Assign value 10  first_array[0] = 10;     // Assign value 20  first_array[1] = 20;     // Printing value  printf("%d %d", first_array[0],                  first_array[1]);  return 0;}
10 20
Dynamic declaration: Malloc is used for dynamically allocating an array. In this situation, the memory will be allocated in heap. Allocated memory will get free after completion of the program.
Example 2:
C
// C program to implement Dynamic Memory Allocation// using Malloc // Importing standard input output files#include<stdio.h>// Include this library for malloc#include<stdlib.h> // Main driver methodint main(){  // Memory will get allocated in  Heap and  // RHS is implicitly typecasted to integer as  // LHS is having integer as a return type  int *first_array = (int*)malloc(sizeof(int)*2);     // Creating and initializing array     // Custom integer values on indices as specified  first_array[0] = 10;  first_array[1] = 20;     // Printing elements of both the arrays  printf("%d %d", first_array[0],                  first_array[1]);  return 0;}
10 20
Now coming to the eccentric goal in figuring an alternative way to allocate the memory instead of Dynamic declaration
Example 3:
C
// C Program to implement Alternative to Malloc // Importing standard input output file#include<stdio.h> // Main driver methodint main(){  // Alternative of Malloc '*' operator refers  // to address of array memory block  int *first_array = (int[2]){};     // Creating and initializing array together     // Custom input element at array indices  first_array[0] = 10;  first_array[1] = 20;     // Printing the array indices as passed in argument  printf("%d %d", first_array[0],                  first_array[1]);  return 0;}
10 20
surindertarika1234
kashishsoda
simranarora5sos
C Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
TCP Server-Client implementation in C
Multithreading in C
Exception Handling in C++
Arrow operator -> in C/C++ with Examples
'this' pointer in C++
UDP Server-Client implementation in C
Understanding "extern" keyword in C
Smart Pointers in C++ and How to Use Them
Multiple Inheritance in C++
How to split a string in C/C++, Python and Java?