Dynamic Memory Allocation

C Programs  for dynamic memory allocations in C
/*code for One Dimensional array dynamic memory allocaion*/
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 5

int main()
{
int *ptr;
ptr=(int *)malloc(MAXSIZE*sizeof(int));

int i;
for(i=0;i<MAXSIZE;i++)
{
printf("ptr[%d]=%d \n",i,i);
}

printf("\n");

return 0;
}
/*
ptr[0]=0
ptr[1]=1
ptr[2]=2
ptr[3]=3
ptr[4]=4
*/

/*
code for 2 D alternate dynamic allocation
*/

#include<stdio.h>
#include<stdlib.h>
#define MAXROW 3
#define MAXCOLUMN 4

int main()
{
int *ptr,i,j;
ptr=(int *)malloc(MAXROW*MAXCOLUMN*sizeof(int));

for(i=0;i<MAXROW;i++)
{
for(j=0;j<MAXCOLUMN;j++)
{
printf("%d+%d=(%d) ",i,j,i+j);
}
printf("\n");
}
printf("\n");
return 0;
}
/*
0+0=(0) 0+1=(1) 0+2=(2) 0+3=(3)
1+0=(1) 1+1=(2) 1+2=(3) 1+3=(4)
2+0=(2) 2+1=(3) 2+2=(4) 2+3=(5)

*/



/*
code for 2-Dimensional array
*/

#include<stdio.h>
#include<stdlib.h>
#define MAXROW 3
#define MAXCOLUMN 4
int main()
{
int **ptr,i,j;

ptr=(int **)malloc(MAXROW*sizeof(int *));

for(i=0;i<MAXROW;i++)
{
ptr[i]=(int *)malloc(MAXCOLUMN*sizeof(int ));
}


for(i=0;i<MAXROW;i++)
{
for(j=0;j<MAXCOLUMN;j++)
{
printf("%d+%d=(%d) ",i,j,i+j);
}
printf("\n");
}

return 0;
}
/*
0+0=(0) 0+1=(1) 0+2=(2) 0+3=(3)
1+0=(1) 1+1=(2) 1+2=(3) 1+3=(4)
2+0=(2) 2+1=(3) 2+2=(4) 2+3=(5)
*/



/*
dynamic memory allocation for 3-Dimensinal array
*/
#include<stdio.h>
#include<stdlib.h>
#define MAXX 2
#define MAXY 3
#define MAXZ 4
int main()
{
int ***ptr,i,j,k;
ptr=(int ***)malloc(MAXX*sizeof(int **));

for(i=0;i<MAXX;i++)
{
ptr[i]=(int **)malloc(MAXX*sizeof(int *));
for(j=0;j<MAXY;j++)
{
ptr[i][j]=(int *)malloc(MAXY*sizeof(int));
}
}

for(i=0;i<MAXX;i++)
{
for(j=0;j<MAXY;j++)
{
for(k=0;k<MAXZ;k++)
{
//printf("%d ",i);
printf("%d+%d+%d=(%d) ",i,j,k,i+j+k);
}
printf("\n");
}
printf("\n");
}

return 0;
}
/*
//when i
0 0 0 0
0 0 0 0
0 0 0 0

1 1 1 1
1 1 1 1
1 1 1 1


//when i+j+k
0+0+0=(0) 0+0+1=(1) 0+0+2=(2) 0+0+3=(3)
0+1+0=(1) 0+1+1=(2) 0+1+2=(3) 0+1+3=(4)
0+2+0=(2) 0+2+1=(3) 0+2+2=(4) 0+2+3=(5)

1+0+0=(1) 1+0+1=(2) 1+0+2=(3) 1+0+3=(4)
1+1+0=(2) 1+1+1=(3) 1+1+2=(4) 1+1+3=(5)
1+2+0=(3) 1+2+1=(4) 1+2+2=(5) 1+2+3=(6)
*/

….


-----

/*
code for dynamic memory allocation for structure
*/
#include<stdio.h>
#include<stdlib.h>
struct node
{
int empid;
char *name;
};
int main()
{
struct node *newnode;
newnode=(struct node *)malloc(sizeof(struct node));
newnode->name=(char *)malloc(sizeof(30));
newnode->empid=1020;
newnode->name="Aditya";

printf("\nempid=%d and name=%s\n\n",newnode->empid,newnode->name);

newnode->empid=1021;
newnode->name="Birla";
printf("\nempid=%d and name=%s\n\n",newnode->empid,newnode->name);

return 0;
}
/*
empid=1020 and name=Aditya

empid=1021 and name=Birla
*/

/*
code Showing PROBLEM of returning local array address and solution
1. static array
2. dynamic memory alllocation
*/

#include<stdio.h>
#include<stdlib.h>//malloc
#include<string.h>//strcpy
char* func1();
char* func11();
char* func2();

int main()
{
char *ptr1;
ptr1=func1();
printf("func1 returnd: %s\n",ptr1);//prints some GARBAGE as array of func1 was local/auto

char *ptr11;
ptr11=func11();
printf("func11 returnd: %s\n",ptr11);//prints Hello India11


char *ptr2;
ptr2=func2();
printf("func2 returnd: %s\n",ptr2);//prints Hello India 2


printf("\n\n");
return 0;
}
/*PROBLEM with returning local array*/
char* func1()
{
char buff1[20];//local array
//buff1="Hello India1";/*error:incompatible types when assigning to type âar[20]ârom type âar *â*/
strcpy(buff1,"HEllo India1");//warning
return (buff1);//warning returning local array is not ok
}

/*SOLUTION 1: use static array*/
char* func11()
{
static char buff11[20];//istatic local array
strcpy(buff11,"HEllo India11");//warning
return (buff11);//returning static local array is okay
}

/*SOLUTION 2: dynamic memory allocation*/
char* func2()
{
char *buff2;
buff2=(char *)malloc(20);
buff2="Hello India2";
return (buff2);/*returning dynamically allocated memory is okay*/
}
/*
func1 returnd: á/c· ú¿
func11 returnd: HEllo India11
func2 returnd: Hello India2
*/


/*
Difference between:
1. char p1[5] //Array of characters
2. char* p2[5] //Array of character pointers
3. char (*p3)[5] //Pointer to Array of character
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
int *ptr[10];//ptr is array of 10 integer pointers
printf("\nptr[0]=%p\n",ptr[0]);//0x8049ff4

int arr[10]={0,1,2,3,4,5,6,7,8,9};

//assgining ptr[0] with the address of arr[0]
*ptr=arr;
//*ptr=arr[0];//warning: assignment makes pointer from integer without a cast
printf("\n ptr[0]=%p \n arr[0]=%p\n",ptr[0],arr);//0x8049ff4

/*with characters */

char p1[5];//p1 is 5-element array of chars
char *p2[5];// p2 is 5-element array of char *
char (*p3)[5];//p3 is pointer to 5-element array of chars

strcpy(p1, "Hello");

p2[0] = (char *) malloc(5 * sizeof(char));
strcpy(p2[0], "India is Great");

p3 = &p1;

printf("\n p1 = %s\np2[0] = %s\np3 = %s\n", p1, p2[0], *p3);
printf("\n");
return 0;
}
/*
___________
p1 |h| e | l | l | o |
---------------


"adi" "mahi" "soni" "jaya" "bangalore"
^          ^        ^           ^      ^
|            |          |            |        |
____________________________________
|0x9999|0x7777|0x3333|0x2222|0x6666|
|___0__|___1__|___2__|___3__|___4__|
p2




________ _____________________
|0x7777| -------> | H | e | l | l | o |
|______| |_0_|_1_|_2_|_3_|_4_|
p3 ^
|
0x7777
*/
/*
ptr[0]=0xb7765fdc

ptr[0]=0xbfe81c34
arr[0]=0xbfe81c34

p1 = Hello
p2[0] = India is Great
p3 = Hello

*/