Find the best tutors and institutes for Programming Languages

Find Best Programming Languages Classes

Please select a Category.

Please select a Locality.

No matching category found.

No matching Locality found.

Search for topics

Dynamic Memory Allocation in C using malloc()

Sankhadeep Pujaru
04 Jun 0 0

#include <stdio.h>
#include <conio.h>
//#include <malloc.h> OR
#include <stdlib.h>
void main()
    int *ptr, i, n, sum = 0;
    printf("how many elements ? ");
        scanf("%d", &n);
    ptr = (int *)malloc(n * sizeof(int));   

  if (ptr == NULL)
    printf("\n Memory not allocated.\n");
    printf("\nEnter the elements : \n");
      for (i=0; i<n; i++)
        scanf("%d", (ptr+i));
        sum += *(ptr + i);        // sum will calculate the summation of n numbers
printf("\n\nSum = %d, and elements : ", sum);
for (i=0; i<n; i++)
    printf("\n %d", *(ptr + i));

free(ptr);   //deallocates the memory previously allocated by a call to malloc()

printf("\n\nSum = %d, and elements : ", sum);
  for (i = 0; i<n; i++)
    printf("\n\n %d", *(ptr + i));    // it will print garbage values as we have deallocated the memory                                                            // using free() call

0 Dislike
Follow 0

Please Enter a comment


Other Lessons for You

Facts about C language
C programming language was developed in 1972 by Dennis Ritchie at AT&T Bell Labs. It was developed to overcome the problems of languages such as B, BPCL. It was developed to write the Unix operating...

Manoj Singh | 4 days ago

0 0

C program for Beginners
A Program to print 2 integer value. #include<stdio.h> #include<conio.h> main() int a,b,add; a=5; b=3; add=a+b; printf(“ Addition=%d”,&add); getch(); Brief description...

Parul R. | 6 days ago

0 0

C for Begginers
C is an procedure oriented programming language. For any begginer the word program is new. Program: Set of instructions to be followed by machine or computer. Instruction Examples: Arithmetic instruction...

Sonali K. | 19 Mar

4 0

Compiling C program on Linux machine GCC ,GDB
Linux have powerful tool called "Terminal". From "terminal" we can command the machine. For any programming language the required tools are editor, compiler and debugger. Compiler will check the syntax...

Narasimha Reddy | 06 Mar

0 0

What is the output of this programming?
#include<stdio.h> main() { printf("%d chennai",+1); }

Asvitha K. | 07 Feb

1 0

Looking for Programming Languages Classes?

Find best tutors for Programming Languages Classes by posting a requirement.

  • Post a learning requirement
  • Get customized responses
  • Compare and select the best

Looking for Programming Languages Classes?

Find best Programming Languages Classes in your locality on UrbanPro

Post your learning requirement is India's largest network of most trusted tutors and institutes. Over 25 lakh students rely on, to fulfill their learning requirements across 1,000+ categories. Using, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 6.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more