<?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>dangling pointer | Student Projects</title>
	<atom:link href="https://studentprojects.in/tag/dangling-pointer/feed/" rel="self" type="application/rss+xml" />
	<link>https://studentprojects.in</link>
	<description>Microcontroller projects, Circuit Diagrams, Project Ideas</description>
	<lastBuildDate>Sat, 10 Dec 2022 04:22:17 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.1.7</generator>
	<item>
		<title>Dangling Pointer</title>
		<link>https://studentprojects.in/software-development/c-tutorials/c-tutorials-c-tutorials/dangling-pointer-2/</link>
					<comments>https://studentprojects.in/software-development/c-tutorials/c-tutorials-c-tutorials/dangling-pointer-2/#respond</comments>
		
		<dc:creator><![CDATA[Shubhajna Rai]]></dc:creator>
		<pubDate>Tue, 25 Oct 2022 09:02:07 +0000</pubDate>
				<category><![CDATA[C Tutorials]]></category>
		<category><![CDATA[dangling pointer]]></category>
		<category><![CDATA[releasing or deleting temporary memory]]></category>
		<category><![CDATA[Function Call]]></category>
		<guid isPermaLink="false">https://studentprojects.in/?p=9706</guid>

					<description><![CDATA[<p>Dangerous pointers are those that point to memory locations that have already been erased or released. During object deletion, dangling pointers are frequently created. When an object with an incoming reference is removed or de-allocated without changing the pointer&#8217;s value, this occurs. The pointer is still pointing at the deallocated memory&#8217;s address in memory. The</p>
<p>The post <a href="https://studentprojects.in/software-development/c-tutorials/c-tutorials-c-tutorials/dangling-pointer-2/">Dangling Pointer</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Dangerous pointers are those that point to memory locations that have already been erased or released. During object deletion, dangling pointers are frequently created. When an object with an incoming reference is removed or de-allocated without changing the pointer&#8217;s value, this occurs. The pointer is still pointing at the deallocated memory&#8217;s address in memory. The system may reallocate the previously deleted memory by itself, which may lead to a number of unexpected outcomes because the memory may now contain different data. The following causes contribute to dangling pointers:</p>



<p><strong>Releasing or deleting temporary memory</strong></p>



<ul><li>The pointer continues to point to freed space even after memory is deallocated. Here&#8217;s an illustration to show how that occurs:</li></ul>



<pre class="wp-block-code"><code lang="c" class="language-c">#include &lt;stdio.h>
int main()
{
    int a = 80;
    int *ptr = (int *)malloc(sizeof(int));
    ptr = &amp;a;
    free(ptr);
    return 0;
}</code></pre>



<p>The code shown above shows how to construct a variable pointer *ptr and an integer variable a with the value 80. The malloc() function is used to generate the pointer variable *ptr. Given that we are aware that the malloc() function produces a void, we type-convert the void pointer into an int pointer using the int * function.</p>



<p><strong>Function Call</strong></p>



<ul><li>We will now examine how the function call causes the pointer to become dangling.</li></ul>



<pre class="wp-block-code"><code lang="c" class="language-c">#include &lt;stdio.h>
int *myvalue()
{
    int a = 10;
    return &amp;a;
}

int main()
{
    int *ptr = myvalue();
    printf(“%d”, *ptr);
    return 0;
}</code></pre>



<p><strong>Output: Segmentation Fault!</strong></p>



<p>The func() method&#8217;s return value is contained in the pointer ptr, which is declared in the main() function of the code above. The programme control is transferred to the context of the int *func when the function func() is called (). The address of the integer variable an is then returned by the method func().</p>



<p>The integer variable an is no longer accessible for the remainder of the program&#8217;s execution at this point, and programme control returns to the main() function. And because it points to a memory location that has been removed or freed from the stack, the pointer ptr becomes dangling. The application consequently causes a segmentation fault.</p>



<p>The outcome would have been 10 had the code been modified and the integer variable, which is static and is declared globally, been local rather than global.</p>



<p>The best way to prevent dangling pointer errors</p>



<p>Our applications get unpleasant defects from the hanging pointer, and these issues frequently lead to security flaws. These errors that result from the generation of a dangling pointer can be prevented by simply initialising the pointer value to NULL. The pointer won&#8217;t then point to the space in memory that was freed. While the purpose of giving the pointer the NULL value was to prevent it from pointing to any previously or arbitrarily designated memory location.</p><p>The post <a href="https://studentprojects.in/software-development/c-tutorials/c-tutorials-c-tutorials/dangling-pointer-2/">Dangling Pointer</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://studentprojects.in/software-development/c-tutorials/c-tutorials-c-tutorials/dangling-pointer-2/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>What is dangling pointer?</title>
		<link>https://studentprojects.in/software-development/c-tutorials/c-faq/dangling-pointer/</link>
					<comments>https://studentprojects.in/software-development/c-tutorials/c-faq/dangling-pointer/#respond</comments>
		
		<dc:creator><![CDATA[Chitra]]></dc:creator>
		<pubDate>Tue, 17 Jan 2012 10:43:23 +0000</pubDate>
				<category><![CDATA[C Questions & Answers]]></category>
		<category><![CDATA[dangling pointer]]></category>
		<category><![CDATA[dangling]]></category>
		<category><![CDATA[pointer]]></category>
		<category><![CDATA[pointers]]></category>
		<category><![CDATA[dangling pointers]]></category>
		<category><![CDATA[pointers dangling]]></category>
		<category><![CDATA[pointer concept]]></category>
		<category><![CDATA[pointers in c]]></category>
		<guid isPermaLink="false">http://studentprojects.in/?p=2601</guid>

					<description><![CDATA[<p>Dangling pointers and wild pointers in computer programming are pointers that do not point to a valid object of the appropriate type. In many applications memory is allocated for holding data objects. After using these objects, tha aplication will de-allocate this memory so that the memory can be re-used. In some cases the alications may</p>
<p>The post <a href="https://studentprojects.in/software-development/c-tutorials/c-faq/dangling-pointer/">What is dangling pointer?</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Dangling pointers and wild pointers in computer programming are pointers that do not point to a valid object of the appropriate type.</p>
<p>In many applications memory is allocated for holding data objects. After using these objects, tha aplication will de-allocate this memory so that the memory can be re-used. In some cases the alications may use a pointer to an object whose memory is already de-allocated. This may lead to application crash or an unpredictable behavior.</p>
<p>scenarios which leads to dangling pointer</p>
<ol>
<li>Application makes use of a object after it has been released, and there by access to an invalid memory location.</li>
<li>A function returns a pointer to one of its local variables, and since this local variable is defined only fro the function, the pointer becomes invalid once the function ends.</li>
</ol>
<p>The most common result of this bug is the crash of the application or its running thread.</p>
<p><strong>Examle 1:</strong></p>
<pre lang="c" escaped="true" line="1">
#include "stdlib.h"
 
void func()
{
    char *dp = malloc(A_CONST);
    /* ... */
    free(dp);         /* dp now becomes a dangling pointer */
    /* ... */
}
</pre>
<p><strong>Example 2:</strong></p>
<pre lang="c" escaped="true" line="1">
{
   char *dp = NULL;
   /* ... */
   {
       char c;
       dp = &amp;c;
   } /* c falls out of scope */
     /* dp is now a dangling pointer */
} 
</pre>
<p><strong>Example 3:</strong></p>
<pre lang="c" escaped="true" line="1">
#include "stdio.h"

int *call();
void main(){

int *ptr;
ptr=call();

fflush(stdin);
printf("%d",*ptr);
}

int * call(){

int x=25;
++x;

return &amp;x;
}
</pre><p>The post <a href="https://studentprojects.in/software-development/c-tutorials/c-faq/dangling-pointer/">What is dangling pointer?</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://studentprojects.in/software-development/c-tutorials/c-faq/dangling-pointer/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
