<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>EmbLogic &#187; Dhannajay Verma</title>
	<atom:link href="https://www.emblogic.com/blog/author/dhannjayv1996/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.emblogic.com/blog</link>
	<description>Embedded System and ARM Training</description>
	<lastBuildDate>Tue, 03 Mar 2020 13:00:06 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.9.1</generator>
	<item>
		<title>Dynamic Memory Allocation</title>
		<link>https://www.emblogic.com/blog/02/dynamic-memory-allocation/</link>
		<comments>https://www.emblogic.com/blog/02/dynamic-memory-allocation/#comments</comments>
		<pubDate>Sat, 29 Feb 2020 06:46:54 +0000</pubDate>
		<dc:creator><![CDATA[Dhannajay Verma]]></dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.emblogic.com/blog/?p=20900</guid>
		<description><![CDATA[DYNAMIC MEMORY ALLOCATION In industry memory management is very important. There are two types of memory which is used by the program, one is Stack segment and another is Heap segment. Stack is a special area of Computer’s memory which &#8230; <a href="https://www.emblogic.com/blog/02/dynamic-memory-allocation/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p align="center"><span style="font-size: x-large"><b>DYNAMIC MEMORY ALLOCATION</b></span></p>
<p style="font-weight: normal" align="justify"><span style="font-size: large">In industry memory management is very important.</span></p>
<p style="font-weight: normal" align="justify"><span style="font-size: large">There are two types of memory which is used by the program, one is<b> Stack segment</b> and another is <b>H</b><b>eap segment.</b></span></p>
<p style="font-weight: normal" align="justify"><span style="font-size: large"><b>Stack</b> is a special area of <b>Computer’s</b> memory which stores the temporary variables created by the functions. In stack variables are stored in run time. It is a temporary storage memory.</span></p>
<p style="font-weight: normal" align="justify"><span style="font-size: large"><b>Heap</b> is a memory used to store global variables. Heap is not managed by the <b>CPU</b> unit. It supports dynamic memory allocation.</span></p>
<p style="font-weight: normal" align="justify"><span style="font-size: large">Dynamic memory allocation is done with malloc(), calloc() and realloc() function.</span></p>
<p style="font-weight: normal" align="justify"><span style="font-size: large"><span style="font-size: large"><span style="text-decoration: underline"><b>Malloc</b></span></span><span style="text-decoration: underline"><b>:</b></span> malloc is used to allocate a block of memory in the heap segment. Malloc allocates the memory at run time. Malloc allocates the memory in the heap segment. Dynamic memory is not cared by the CPU, so it’s required to free the memory after the use to prevent the memory leakage. </span></p>
<p align="justify"><span style="font-size: large"><b><span style="font-size: large">Syntax</span>:</b></span></p>
<p align="justify"><span style="font-size: large"><b>ptr=(int*/char*)malloc(byte-size);</b></span></p>
<p style="font-weight: normal" align="justify"><span style="font-size: large">where<b> ptr</b> is a pointer type of variable which points the base address of the allocated memory in heap segment. After that is pointer typecast of the data.</span></p>
<p style="font-weight: normal" align="justify"><span style="font-size: large">If we allocates the ‘0’ bytes to the malloc then also it reserve some memory which is of size 32Kbytes.</span></p>
<p style="font-weight: normal" align="justify"><span style="font-size: large">The usable size of the malloc is checked by the malloc_usable_size which is 24Kbytes, and it keeps extra 8bytes with it which is chunk size. </span></p>
<p align="justify"><span style="font-size: large"><span style="text-decoration: underline"><b>Working of malloc function:</b></span></span></p>
<p style="font-weight: normal" align="justify"><span style="font-size: large">When we gives the size to allocate then it checks the threshold of malloc and then checks it in memory pages if available then then it allocates the memory blocks otherwise memory mapping comes into the picture(mmap).</span></p>
<p style="font-weight: normal" align="justify"><span style="font-size: large">Malloc allocates the memory in heap segment which have fixed size of 130Kbytes.</span></p>
<p style="font-weight: normal" align="justify"><span style="font-size: large">When we allocates the memory then it points the address of the page which is of 130Kbyte’s size if the requirement is more than heap then the size of heap then it calls the extra memory by mmap.</span></p>
<p style="font-weight: normal" align="justify"><span style="font-size: large">On the successful of malloc it returns the address of the allocated space. On the failure of malloc it returns the NULL .</span></p>
<p style="font-weight: normal" align="justify"><span style="font-size: large"><span style="font-size: large"><span style="text-decoration: underline"><b>Calloc</b></span></span><span style="text-decoration: underline"><b>:</b></span> calloc is used to allocate dynamic memory. Dynamic memory is allocated on run time .</span></p>
<p style="font-weight: normal" align="justify"><span style="font-size: large">Calloc is similar to malloc the difference is, that the default values of the allocated blocks are zero in calloc and in malloc the default values are garbage. </span></p>
<p align="justify"><span style="font-size: large"><span style="text-decoration: underline"><b><span style="font-size: large">Realloc</span>:</b></span><span style="font-weight: normal">realloc is used to allocate the memory on run time whenever the extra memory required on the run time.</span></span></p>
<p align="justify"><span style="font-size: large"><span style="text-decoration: underline"><b><span style="font-size: large">Syntax</span>:</b></span></span></p>
<p style="font-weight: normal" align="justify"><span style="font-size: large">ptr_new=(typr-caste)realloc(ptr,new_size in bytes);</span></p>
<p align="justify"><span style="font-size: large"><span style="font-weight: normal">where </span><b>ptr_new</b><span style="font-weight: normal"> is the address of the new allocate space, </span><b>ptr</b><span style="font-weight: normal"> is the previous size of allocated memory.</span></span></p>
<p align="justify"><span style="font-size: large"><span style="text-decoration: underline"><b>Working of realloc function:</b></span></span></p>
<p style="font-weight: normal" align="justify"><span style="font-size: large">when realloc function is used then first it checks the available space in that page if the required space is available then it added the requested blocks of memory.</span></p>
<p style="font-weight: normal" align="justify"><span style="font-size: large">If the size is not available then an extra page comes through the memory mapping concept and reallocates the space requested and it copies the data of the current address to new allocated space and frees the previous memory.</span></p>
<p style="font-weight: normal" align="justify"><span style="font-size: large">Malloc is slightly faster than Calloc than Realoc. </span></p>
<p style="font-weight: normal" align="justify">
<p style="font-weight: normal" align="justify">
<p style="font-weight: normal" align="justify">
<p style="font-weight: normal" align="justify">
]]></content:encoded>
			<wfw:commentRss>https://www.emblogic.com/blog/02/dynamic-memory-allocation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
