UrbanPro
true

Learn Programming Languages from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Dynamic Memory Allocation in C using malloc()

Sankhadeep Pujaru
04/06/2020 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");
  else
  {
    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
getch();
}

0 Dislike
Follow 2

Please Enter a comment

Submit

Other Lessons for You

4 Things Every Tech Startup Needs to Know About The Coaching Industry
Knowledge on any subject is widely available to those who wish to learn. However, just gathering knowledge from other people doesn’t guarantee results in business. Results come from applying what...

Why C is a Language and not a database?
When I interviewed a candidate, I raised this question many times, but I have not got the answer correctly. To under why C is a language and not a database, it is good to understand why our communication...

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

Variables
Variables in C Language:A variable is a name that may be used to store a data value. Unlike constant, variables are changeable, we can change value of a variable during execution of a program. A programmer...

How to become a self-taught programmer?
In this lesson, I will teach you the step by step process to becoming a self-taught programmer by learning all the programming concepts by yourself with the help of online resources. Step 1: Choose your...
A

Abraham Paul

1 0
0
X

Looking for Programming Languages Classes?

The best tutors for Programming Languages Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Programming Languages with the Best Tutors

The best Tutors for Programming Languages Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. 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