1-D MEMORY ALLOCATION IN C :-
1. All pointers in C & C++ is of 4 Bytes on 32 Bit system.
2. In C, Dynamic Memory Allocation is done by malloc( ), calloc( ), realloc( ) & free ( ).
3. malloc( ) takes only single arguement ( the amount of memory to allocate in bytes ).
4. malloc( ) does not initialize the memory allocated, they contains garbage values.
5. calloc( ) needs two arguements ( the no. of variables to allocate in memory, & the size in bytes of single variable ).
6. calloc( ) initialize the allocated memory by Zero.
7. free( ) is used to free all the allocated memory which is allocated by the pointer at the time of execution.
(1). char Array.
char* nameofarray = (char*)malloc( memorywanttoallocate * sizeof(char*) );
free(nameofarray); // this line will free all memory that is allocated by malloc( ).
(2). int Array.
int* nameofarray = (int*)malloc( memorywanttoallocate * sizeof(int*));
free(nameofarray); // this line will free all memory that is allocated by malloc( ).
(3). float Array.
float* nameofarray = (float*)malloc(memorywanttoallocate * sizeof(float*));
free(nameofarray); // this line will free all memory that is allocated by malloc( ).
(4). double Array.
double* nameofarray = (double*)malloc(memorywanttoallocate * sizeof(double*));
free(nameofarray); // this line will free all memory that is allocated by malloc( ).
(1). char Array.
char** nameofarray = (char**)malloc( HeightofMatrix * sizeof(char**) );
for( int i = 0; i < HeightofMatrix; i++ )
{
nameofarray[ i ] = (char*)malloc( WidthofMatrix * sizeof(char*));
}
free(nameofarray); // this line will free all memory that is allocated by malloc( ).
(2). int Array.
int** nameofarray = (int**)malloc( HeightofArray * sizeof(int**));
for( int i = 0; i < HeightofMatrix; i++ )
{
nameofarray[ i ] = (int*)malloc( WidthofMatrix * sizeof(int*));
}
free(nameofarray); // this line will free all memory that is allocated by malloc( ).
(3). float Array.
float** nameofarray = (float**)malloc( * sizeof(float**));
for( int i = 0; i < HeightofMatrix; i++ )
{
nameofarray[ i ] = (float*)malloc( WidthofMatrix * sizeof(float*));
}
free(nameofarray); // this line will free all memory that is allocated by malloc( ).
(4). double Array.
double** nameofarray = (double**)malloc(memorywanttoallocate * sizeof(double*));
for( int i = 0; i < HeightofMatrix; i++ )
{
nameofarray[ i ] = (double*)malloc( WidthofMatrix * sizeof(double*));
}
free(nameofarray); // this line will free all memory that is allocated by malloc( ).
1. All pointers in C & C++ is of 4 Bytes on 32 Bit system.
2. In C, Dynamic Memory Allocation is done by malloc( ), calloc( ), realloc( ) & free ( ).
3. malloc( ) takes only single arguement ( the amount of memory to allocate in bytes ).
4. malloc( ) does not initialize the memory allocated, they contains garbage values.
5. calloc( ) needs two arguements ( the no. of variables to allocate in memory, & the size in bytes of single variable ).
6. calloc( ) initialize the allocated memory by Zero.
7. free( ) is used to free all the allocated memory which is allocated by the pointer at the time of execution.
(1). char Array.
char* nameofarray = (char*)malloc( memorywanttoallocate * sizeof(char*) );
free(nameofarray); // this line will free all memory that is allocated by malloc( ).
(2). int Array.
int* nameofarray = (int*)malloc( memorywanttoallocate * sizeof(int*));
free(nameofarray); // this line will free all memory that is allocated by malloc( ).
(3). float Array.
float* nameofarray = (float*)malloc(memorywanttoallocate * sizeof(float*));
free(nameofarray); // this line will free all memory that is allocated by malloc( ).
(4). double Array.
double* nameofarray = (double*)malloc(memorywanttoallocate * sizeof(double*));
free(nameofarray); // this line will free all memory that is allocated by malloc( ).
2-D MEMORY ALLOCATION IN C :-
(1). char Array.
char** nameofarray = (char**)malloc( HeightofMatrix * sizeof(char**) );
for( int i = 0; i < HeightofMatrix; i++ )
{
nameofarray[ i ] = (char*)malloc( WidthofMatrix * sizeof(char*));
}
free(nameofarray); // this line will free all memory that is allocated by malloc( ).
(2). int Array.
int** nameofarray = (int**)malloc( HeightofArray * sizeof(int**));
for( int i = 0; i < HeightofMatrix; i++ )
{
nameofarray[ i ] = (int*)malloc( WidthofMatrix * sizeof(int*));
}
free(nameofarray); // this line will free all memory that is allocated by malloc( ).
(3). float Array.
float** nameofarray = (float**)malloc( * sizeof(float**));
for( int i = 0; i < HeightofMatrix; i++ )
{
nameofarray[ i ] = (float*)malloc( WidthofMatrix * sizeof(float*));
}
free(nameofarray); // this line will free all memory that is allocated by malloc( ).
(4). double Array.
double** nameofarray = (double**)malloc(memorywanttoallocate * sizeof(double*));
for( int i = 0; i < HeightofMatrix; i++ )
{
nameofarray[ i ] = (double*)malloc( WidthofMatrix * sizeof(double*));
}
free(nameofarray); // this line will free all memory that is allocated by malloc( ).