20120113

How to use malloc function in C

//Program in c to learn malloc function, mallocinc.c
How to use malloc in C. Simple Program for malloc in C.

->malloc function allocates required amount of memory in heap (instead of stack) and
returns the pointer to the address of allocated block of memory.
->We need to type cast the returned pointer by malloc as it returns a void pointer.
->memory allocated by malloc in heap is deallocated by free, so that memory in heap can be
  allocated by other malloc functions.
->Syntax of malloc in C
datatype *p;
p=(datatype *)malloc(sazeof(datatype));
example of malloc:
int *p;
p=(int *)malloc(100*sizeof(int));

Explanation:
Here, malloc function allocates 400( 100*size of integer ie 100*4=400 ) bytes in heap and
assigns the address of the allocated memory in integer pointer p.
How to use malloc in C.
Simple Program for malloc in C.
#include<stdio.h>
int main
{
int *p;
p=(int *)malloc(sizeof(int));
*p=10;
printtf("%d",*p);
free(p);
return 0;
}


//Program in c to learn malloc function, mallocinc.c
//Program in c to learn malloc function, mallocinc.c
//Program in c to learn malloc function, mallocinc.c
//Program in c to learn malloc function, mallocinc.c

#include<stdio.h>
int main()
{
char name[5];
int i,len;
char *p;
char *pname[5];
for(i=0;i<=5;i++)
        {
        printf("Enter a name: ");
        scanf("%s", name);
        len=strlen(name);
        p=(char *)malloc(len-1);
        strcpy(p,name);
        pname[i]=p;
        }

        for(i=0;i<=5;i++)
        {
        printf("%s",pname[i]);
        }
return 0;
}

//How to use malloc function in C, mallocinc.c
//How to use malloc function in C, mallocinc.c
//How to use malloc function in C, mallocinc.c
//How to use malloc function in C, mallocinc.c


Previous                             Home                               Next

4 comments:

  1. How to use malloc function in C
    How to use malloc function in C
    How to use malloc function in C
    How to use malloc function in C
    How to use malloc function in C
    How to use malloc function in C
    How to use malloc function in C
    How to use malloc function in C
    How to use malloc function in C
    How to use malloc function in C
    How to use malloc function in C
    How to use malloc function in C

    ReplyDelete
  2. How to use malloc function in C
    How to use malloc function in C
    How to use malloc function in C
    How to use malloc function in C
    How to use malloc function in C
    How to use malloc function in C
    How to use malloc function in C
    How to use malloc function in C
    How to use malloc function in C
    How to use malloc function in C
    How to use malloc function in C
    How to use malloc function in C

    ReplyDelete
  3. //C program to use malloc
    //How to use malloc in C
    int main()
    {
    // allocate memory for the string
    char *string1 = (char *) malloc(100*sizeof(char));

    // write some data to the string
    strcpy(string1, "Hello world!");

    // display the string
    printf("%s\n", string1);

    // remove the string from memory
    free(string1);

    return 0;
    }


    //C program to use malloc
    //How to use malloc in C
    //C program to use malloc
    //How to use malloc in C
    //C program to use malloc
    //How to use malloc in C

    ReplyDelete
  4. //Program in c++ to learn new
    //with char array
    #include
    int main()
    {
    char name[10]="abcdef";
    cout<<("\n You entered: \n", name);

    char *p;
    p=new char[strlen(name)]- 1;
    // Note the Sysntax
    strcpy(p,name);
    cout<<("\n copied string is: \n", name);

    return 0;
    }

    ReplyDelete