what is mallloc()?how it works?

Answer Posted / vignesh1988i

malloc is briefly termed as Memory ALLOCation ......
whenever we write a program in C we will declare some
variables or arrays for a size more than whatever we need ,
etc etc..... so some variables are getting used correctly
and in arrays only some of the memory spaces are getting
used because the user dont know how much spaces he is gonna
use.......so here some memory is getting wasted without
using it.... so there comes a concept called DYNAMIC MEMORY
ALLOCATION in C...

we will give the values for how much memory we need at the
run time of the program... but this concept also only 85%
efficient... this has three types :
1)malloc();
2)calloc();
3)realloc();
malloc() is a function present in ALLOC.H standard library
which is used to allocate memory at run time (ie) in single
or in blocks.....

the syntax is :
pointer_variable=(typecasting
datatype*)malloc(sizeof(datatype));

this pointer variable should be of same datatype as the
typecasting datatype........

thank u

Is This Answer Correct ?    4 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the purpose of 'register' keyword in c language?

633


When should structures be passed by values or by references?

588


I need testPalindrome and removeSpace #include #define SIZE 256 /* function prototype */ /* test if the chars in the range of [left, right] of array is a palindrome */ int testPalindrome( char array[], int left, int right ); /* remove the space in the src array and copy it over to the "copy" array */ /* set the number of chars in the "copy" array to the location that cnt points t */ void removeSpace(char src[], char copy[], int *cnt); int main( void ) { char c; /* temporarily holds keyboard input */ char string[ SIZE ]; /* original string */ char copy[ SIZE ]; /* copy of string without spaces */ int count = 0; /* length of string */ int copyCount; /* length of copy */ printf( "Enter a sentence:\n" ); /* get sentence to test from user */ while ( ( c = getchar() ) != '\n' && count < SIZE ) { string[ count++ ] = c; } /* end while */ string[ count ] = '\0'; /* terminate string */ /* make a copy of string without spaces */ removeSpace(string, copy, ©Count); /* print whether or not the sentence is a palindrome */ if ( testPalindrome( copy, 0, copyCount - 1 ) ) { printf( "\"%s\" is a palindrome\n", string ); } /* end if */ else { printf( "\"%s\" is not a palindrome\n", string ); } /* end else */ return 0; /* indicate successful termination */ } /* end main */ void removeSpace(char src[], char copy[], int *cnt) { } int testPalindrome( char array[], int left, int right ) { }

2220


Can math operations be performed on a void pointer?

593


What is adt in c programming?

618






What is time null in c?

589


What are categories used for in c?

575


What is a file descriptor in c?

570


The purpose of this exercise is to benchmark file writing and reading speed. This exercise is divided into two parts. a). Write a file character by character such that the total file size becomes approximately >10K. After writing close the file handler, open a new stream and read the file character by character. Record both times. Execute this exercise at least 4 times b). Create a buffer capable of storing 100 characters. Now after generating the characters, first store them in the buffer. Once the buffer is filled up, store all the elements in the file. Repeat the process until the total file size becomes approximately >10K.While reading read a while line, store it in buffer and once buffer gets filled up, display the whole buffer. Repeat the exercise at least 4 times with different size of buffer (50, 100, 150 …). Records the times. c). Do an analysis of the differences in times and submit it in class.

1639


What is array of structure in c?

608


What is the g value paradox?

652


What is difference between arrays and pointers?

589


What is function in c with example?

642


What is pragma c?

621


How can I direct output to the printer?

822