C_82 What is Dangling pointer in C | C Language Tutorials

Jenny's Lectures CS IT
27 Aug 202124:51

Summary

TLDRThis video discusses special pointers in C, focusing on the concept of dangling pointers. It explains what a dangling pointer is, the causes, and how to prevent issues related to them. Examples and code demonstrations are provided to illustrate how pointers can point to freed memory locations, leading to undefined behavior and potential bugs. The video also highlights good practices to avoid dangling pointers, such as setting pointers to null after freeing memory. Additionally, there's information about a scholarship test for GATE aspirants organized by An Academy.

Takeaways

  • 📌 A dangling pointer occurs when a pointer still points to a memory location after it has been freed.
  • 📌 Dangling pointers can lead to unpredictable behavior, including program crashes or garbage values.
  • 📌 The `malloc` function dynamically allocates memory in C, and its memory should be freed using the `free` function to avoid memory leaks.
  • 📌 Always set a pointer to `NULL` after freeing its memory to prevent it from becoming a dangling pointer.
  • 📌 Checking if a pointer is `NULL` before dereferencing it is a good programming practice to avoid errors.
  • 📌 A pointer becomes dangling if it points to a local variable that goes out of scope.
  • 📌 Returning the address of a local variable from a function can create a dangling pointer because the local variable's memory is freed once the function returns.
  • 📌 Static variables retain their memory throughout the program's execution, preventing them from becoming dangling pointers.
  • 📌 The `sizeof` operator in C returns the size of a data type, which is useful for memory allocation.
  • 📌 Using `static` keyword for variables ensures they have a global lifetime and do not get deallocated when going out of scope.

Q & A

  • What is a dangling pointer in C?

    -A dangling pointer in C is a pointer that continues to point to a memory location after the memory has been freed or deallocated. Accessing this memory can lead to undefined behavior, such as program crashes or unexpected results.

  • What are the main causes of dangling pointers?

    -The main causes of dangling pointers include deallocating memory using the free() function while still holding the pointer, using local variables' addresses outside their scope, and returning addresses of local variables from functions.

  • How can you prevent dangling pointers after freeing memory?

    -To prevent dangling pointers after freeing memory, you can set the pointer to NULL immediately after freeing the memory. This ensures that the pointer does not point to an invalid memory location.

  • What happens if you dereference a dangling pointer?

    -Dereferencing a dangling pointer can lead to undefined behavior, such as accessing invalid or garbage data, causing program crashes, or other unpredictable results.

  • How does dynamic memory allocation work with malloc()?

    -Dynamic memory allocation using malloc() involves requesting a block of memory from the heap. The malloc() function returns a pointer to the beginning of the allocated memory block. The size of the memory block is specified in the malloc() call.

  • What is the purpose of typecasting the return value of malloc()?

    -The malloc() function returns a void pointer, which is a generic pointer type. To use the allocated memory for a specific data type, you need to typecast the void pointer to the appropriate type, such as int*, float*, etc.

  • Why is it important to check if a pointer is NULL before dereferencing it?

    -It is important to check if a pointer is NULL before dereferencing it to prevent dereferencing an invalid memory location. Dereferencing a NULL pointer typically leads to a program crash or segmentation fault.

  • What can happen if you use the address of a local variable outside its scope?

    -Using the address of a local variable outside its scope can lead to dangling pointers because the memory for the local variable is deallocated when the function or block ends. Accessing this memory outside the scope can result in undefined behavior.

  • How can declaring a variable as static affect its scope and prevent dangling pointers?

    -Declaring a variable as static extends its lifetime to the entire duration of the program. This means the memory for the static variable is not deallocated when the function or block ends, thus preventing dangling pointers when returning its address.

  • What is a good practice to follow when working with pointers to avoid bugs and crashes?

    -A good practice when working with pointers is to always initialize pointers to NULL if they are not immediately assigned a valid memory address. Additionally, before dereferencing a pointer, always check if it is NULL to avoid accessing invalid memory locations.

Outlines

00:00

🖥️ Introduction to Dangling Pointers

This paragraph introduces the concept of dangling pointers in C programming. It explains what a dangling pointer is, its causes, and how to address issues arising from them. The speaker mentions an example using dynamic memory allocation and highlights the significance of understanding this topic for GATE examination aspirants. They also promote a weekly GATE scholarship test conducted by Unacademy, encouraging viewers to participate for better preparation.

05:00

🔍 Understanding Dynamic Memory Allocation

The paragraph delves deeper into dynamic memory allocation using the malloc function in C. An example is provided where a pointer is used to dynamically allocate memory for an integer. The process is explained step-by-step, including typecasting the void pointer returned by malloc to an integer pointer. The importance of correctly managing memory to avoid issues is emphasized.

10:01

📉 Freeing Memory and Dangling Pointers

This section explains what happens when dynamically allocated memory is freed. It shows that after freeing the memory, the pointer still holds the address of the freed memory, thus becoming a dangling pointer. The potential consequences of dereferencing a dangling pointer, such as unpredictable behavior and program crashes, are discussed. The analogy of having an outdated phone number is used to illustrate the concept.

15:03

🔧 Handling Dangling Pointers

This paragraph provides solutions to avoid dangling pointers. It advises setting pointers to NULL after freeing memory to prevent accidental dereferencing. The importance of checking if a pointer is NULL before dereferencing it is highlighted as a good practice to avoid program crashes and other issues. The concept of memory allocation and deallocation in the stack and heap is revisited to reinforce understanding.

20:05

🔄 Examples of Dangling Pointers

Further examples are provided to illustrate dangling pointers. One example shows how a local variable's address is returned from a function, leading to a dangling pointer once the function scope ends. Another example demonstrates how a pointer becomes dangling when it points to a local variable that goes out of scope. The importance of using static variables to extend the lifetime of variables is also discussed.

Mindmap

Keywords

💡Pointers in C

Pointers in C are a fundamental concept in the C programming language that allows programmers to work with the memory addresses of variables. They are used to dynamically allocate memory, pass data to functions, and manipulate data more efficiently. In the video, pointers are the central theme, with a focus on 'dangling pointers' as a specific type of pointer error.

💡Dangling Pointer

A dangling pointer is a pointer that points to a memory location that has been freed or is no longer valid. It is a common source of bugs and undefined behavior in programs. The video script discusses the causes of dangling pointers, such as freeing memory that a pointer is referencing, and how to avoid them by setting pointers to NULL after freeing the memory.

💡Dynamic Memory Allocation

Dynamic memory allocation is the process of allocating memory during the runtime of a program using functions like 'malloc'. It is contrasted with static memory allocation, which is done at compile time. The script uses 'malloc' to demonstrate how memory can be allocated to a pointer, which later becomes a dangling pointer if not handled correctly.

💡Malloc Function

The malloc function is a standard library function in C used for dynamic memory allocation. It allocates a block of memory of a specified size and returns a void pointer to the beginning of the block. In the script, malloc is used to allocate memory for an integer, which the pointer then references.

💡Free Function

The free function is used to deallocate memory that was previously allocated using malloc, allowing the memory to be used for other purposes. The script warns about the dangers of using a pointer after the memory it points to has been freed, which would make the pointer a dangling pointer.

💡NULL Pointer

A NULL pointer is a pointer that does not point to any valid memory location. It is often used to indicate the absence of a valid pointer value. The script suggests setting a pointer to NULL after freeing its associated memory to prevent it from becoming a dangling pointer.

💡Undefined Behavior

Undefined behavior in programming occurs when a program does something that the language's standard does not define or guarantee. In the context of the video, dereferencing a dangling pointer can lead to undefined behavior, such as program crashes or unpredictable outputs.

💡Scope of Variables

The scope of a variable defines the region of the code where the variable is accessible. In the script, the concept of scope is used to explain how a pointer can become a dangling pointer when it references a local variable that goes out of scope.

💡Static Variables

Static variables maintain their value throughout the lifetime of a program, even when they are out of scope. The script contrasts static variables with local variables, noting that a pointer to a static variable does not become a dangling pointer, unlike a pointer to a local variable.

💡GATE Examination

The GATE (Graduate Aptitude Test in Engineering) Examination is a standardized test used for admissions to postgraduate programs in India. The script mentions an academy conducting a weekly scholarship test for GATE aspirants, which is a competition to help students prepare for the exam.

💡Heap Memory

Heap memory is a region of a computer's storage used for dynamic memory allocation. The script explains that malloc allocates memory from the heap, and if not managed correctly, can lead to dangling pointers when the allocated memory is freed.

Highlights

Introduction to pointers in C programming, specifically focusing on dangling pointers.

Explanation of what a dangling pointer is and the causes of dangling pointers.

Discussion on the errors that can occur in a program due to dangling pointers.

Illustration of dangling pointers through an example using dynamic memory allocation with malloc.

Demonstration of how to allocate memory dynamically and how a pointer can become a dangling pointer after freeing the allocated memory.

Explanation of how to handle dangling pointers by setting them to NULL after freeing the memory.

Introduction of a contest for GATE aspirants and its benefits for preparation.

Description of the contest format, including the number of questions and the time limit.

Mention of the live leaderboard feature in the contest to analyze rankings.

Highlight of the detailed video solutions provided after each contest to help contestants identify weak areas.

Promotion of the contest and the use of a special code (jkl10) for enrollment.

Explanation of the concept of a null pointer and its role in avoiding dereferencing dangling pointers.

Discussion on the importance of checking if a pointer is NULL before dereferencing it.

Example of a dangling pointer caused by a local variable going out of scope.

Demonstration of how a pointer can become a dangling pointer when it points to a local variable in a function that has returned.

Explanation of the difference between local and static variables in the context of dangling pointers.

Practical demonstration of a dangling pointer in a code example and the unpredictable behavior it can cause.

Conclusion and preview of the next topic to be discussed in the series, which is the wild pointer.

Transcripts

play00:00

so in this phase of learning programming

play00:01

in c we are discussing some special

play00:02

pointers in c we have discussed void and

play00:05

null pointer in this video i'll talk

play00:06

about dangling pointer everything about

play00:08

this pointer like what is dangling

play00:10

pointer the cause of dangling pointers

play00:12

and how to get rid of the situation how

play00:14

to get rid of dangling pointers and if

play00:15

dangling pointer comes in your program

play00:17

then what type of error you can get

play00:20

we will discuss with the help of program

play00:22

or you can say with example i'll show

play00:23

you code also on my laptop right but

play00:26

before that i just want to tell you one

play00:27

thing if you are preparing for gate

play00:29

examination or will be preparing for

play00:31

gate examination then an academy is

play00:33

going to conduct a combat it's like

play00:36

india's largest weekly you know

play00:38

competition for gate aspirants so you

play00:39

can see weekly scholarship test for gate

play00:41

aspirants

play00:43

here it would be on 29th of august the

play00:46

time limit would be 60 minutes and 20

play00:48

questions you will get and the questions

play00:50

has been curated by the an academy's

play00:52

best gate educators so here you will get

play00:55

challenging and original gate questions

play00:57

so better to take it live to compete

play00:59

with the

play01:00

best if you take it live then all india

play01:02

live leaderboard is also there

play01:04

just to you can analyze your ranking

play01:07

using this letterboard because after

play01:09

every questions of this

play01:10

contest it will show your ranking so you

play01:14

know where you are standing among all

play01:16

other you know competitors

play01:19

so you can assess your gate preparation

play01:20

in just 60 minutes and one more thing

play01:23

after every contest detailed video

play01:26

solution would also be given to you by

play01:27

the experts so you can you know you can

play01:30

identify your weak areas and you can

play01:32

improve accordingly and so that you you

play01:34

get it right next time

play01:36

and if you take it live you also get a

play01:38

chance to win some exciting prizes you

play01:40

can also see the detail over here and

play01:43

it's free you don't need to pay anything

play01:44

you just have to enroll for it and use

play01:46

my code jkl10 for enrollment so

play01:49

the enrollment link i'll put in the

play01:51

description box of this video you can go

play01:53

and check out so now let's see what is

play01:55

this pointer before going to the

play01:56

definition directly i just want to show

play01:58

you one example

play02:00

here i am going to use dynamic memory

play02:01

allocation using mallow function right

play02:04

suppose here i am taking a pointer

play02:06

in

play02:09

and dynamically i am going to allocate

play02:10

memory here

play02:12

right so suppose i am allocating memory

play02:14

here

play02:15

this would be syntax of

play02:17

malloc function and here suppose i am

play02:20

writing size of

play02:22

int

play02:25

if you are not getting the syntax its

play02:27

okay we will discuss everything about

play02:28

malloc and kellogg in later videos

play02:32

for this video just you have to you know

play02:34

understand this thing

play02:36

i am going to ask

play02:38

from the malloc function

play02:40

right

play02:41

this is built in function that please

play02:43

allocate me

play02:45

a memory dynamically dynamically means

play02:48

from heap

play02:50

right

play02:52

how much memory size of in size of wind

play02:54

means four bytes means four bytes

play02:57

allocate me four bytes and mellow

play02:59

return void pointer but that is why i i

play03:03

am typecasting it that is why i am

play03:05

writing here in star

play03:08

right because pointer is what type is

play03:09

what in so that is why it is returning

play03:12

word pointer so i am type casting it

play03:15

into in

play03:16

right that that's it this is the you

play03:18

know meaning of this line dynamically we

play03:21

are going to allocate memory to pointer

play03:24

and suppose here

play03:25

uh maybe uh

play03:27

in this memory now

play03:29

in s3 i am storing 10.

play03:33

right so here it's like something like

play03:35

this here we have ptr

play03:37

and dynamically suppose memory has been

play03:39

allocated and sizes

play03:42

size of int that is 4 byte so you can

play03:45

say

play03:46

4 bytes 1 2 3 4

play03:49

and address is suppose 100 100 and 100

play03:52

203

play03:55

and it will return word pointer but i

play03:56

have typecasted it so it will return

play03:58

what

play03:59

the obviously the base address of the

play04:01

address of the first byte so 100 has

play04:03

been stored in

play04:04

the address over the first byte has been

play04:06

stored in this point and now pointer is

play04:08

pointing to this memory location

play04:10

i hope you got the meaning of this line

play04:11

now now in a strict ptr means value at

play04:14

this is i'm accessing value

play04:17

at

play04:18

this point in pointer i have 100 so

play04:20

value at 100

play04:22

means here

play04:23

i want to store so i just i'm not

play04:26

representing it something like this i'm

play04:28

representing it with single block size

play04:30

uh sorry address is 100 so i'm storing

play04:32

here 10

play04:34

right now if you want to print like

play04:37

printf

play04:38

and

play04:39

i just want to print a strict ptr

play04:41

whatever the value i want to access this

play04:43

value so it will give you 10

play04:46

right but

play04:48

if after this i have done something with

play04:51

this pointer and after this i want to

play04:53

free this ptr

play04:56

it means

play04:58

it is basically used to free

play05:00

the memory

play05:02

means we have

play05:03

freed this memory

play05:05

now

play05:05

there is nothing here we have freed this

play05:08

memory right we have deleted this memory

play05:11

space we have freed this point

play05:14

but still yeah in pointer we have

play05:16

hundred

play05:17

that is address of this in pointer we

play05:19

have but actually we have deleted this

play05:22

memory

play05:24

right

play05:24

this is no more reserved now

play05:28

it is in what

play05:30

free block of the memory

play05:32

one is consumed one memories one part is

play05:35

consumed memory one part is free memory

play05:38

right when you you know run a program

play05:40

then obviously some memory has been

play05:42

allocated to you from stack also and if

play05:44

you

play05:45

allocate memory dynamically then from

play05:47

heap also

play05:48

so

play05:49

here we have

play05:51

two parts free memory and

play05:54

consumed memory

play05:56

so once we have freed this this block is

play05:59

now in free memory

play06:01

right so

play06:02

and

play06:03

before freeing this it wasn't consumed

play06:06

one here so at that time it was having

play06:09

some valid address

play06:11

like hundred

play06:12

but we have now freed this so now it is

play06:14

in free block and all the memory block

play06:16

here are having

play06:18

invalid address right but this pointer

play06:21

is still pointing to here

play06:24

we haven't updated this thing

play06:26

so now at this time this pointer is

play06:29

acting as dangling pointer so now it is

play06:32

what now

play06:34

dangling pointer

play06:37

here if you will dereference it like

play06:39

printf percentage d and after freeing if

play06:42

you will dereference it

play06:45

it means

play06:47

value at this address but here we don't

play06:49

have anything we have deleted this

play06:51

address with this memory space so it

play06:54

will give you some garbage value or

play06:55

maybe some undefined behavior it will

play06:57

show

play06:58

unpredictable behavior we cannot say

play07:00

maybe program will crash or it will give

play07:02

some garbage we don't know

play07:04

because it is now dangling pointer

play07:06

dangling is what in simple terms you

play07:09

will say like

play07:10

it's hanging

play07:12

it is still hanging

play07:14

right

play07:15

but we don't have anything here

play07:18

let's take one example if you have

play07:20

suppose

play07:21

if you have a

play07:23

phone number of your friend

play07:26

but

play07:27

he has changed

play07:29

his phone number but you still have

play07:32

that old

play07:33

phone number

play07:34

right

play07:35

so that phone number is like a pointer

play07:38

you can access the your friend using

play07:41

that phone number you can call your

play07:42

friend using that phone number but but

play07:44

now

play07:45

that phone number doesn't exist

play07:47

because he has changed his phone number

play07:50

so now that phone number is now it's

play07:53

like dangling pointer

play07:56

right

play07:56

you cannot access you cannot call your

play07:58

friend using that phone number now if

play08:00

you will call

play08:02

then what would happen we don't know

play08:04

maybe that has been allocated to some

play08:06

other

play08:07

person

play08:08

right or maybe that is switched off or

play08:11

we don't know anything

play08:13

can be possible so here also maybe this

play08:15

memory

play08:16

after some time maybe that freed memory

play08:18

has been allocated to do some other you

play08:20

know process or some other variable or

play08:22

some other kind of thing

play08:24

and after that later if you will

play08:26

de-reference it definitely it will show

play08:28

some undefined behavior or it can cause

play08:30

some bug in your program

play08:32

right

play08:34

it's like a risky for your you know

play08:36

security purpose

play08:38

right so don't leave any pointer

play08:43

hanging or dangling pointer so what is

play08:45

the situation how to get rid of the

play08:47

situation

play08:49

after freeing this

play08:50

what you can do

play08:54

ptr is equal to

play08:57

null

play08:58

better to initialize ptr with null so

play09:01

now in ptr we have null that is zero

play09:04

now null pointer we have already

play09:05

discussed now it is pointing to it is it

play09:08

is not pointing to any valid address and

play09:10

we cannot dereference this null pointer

play09:12

and obviously a good practice is what

play09:14

further if in a program you will use

play09:16

first of all we will check if

play09:18

like pointer is equal to is equal to

play09:20

null simply print we cannot de-reference

play09:23

it hence

play09:25

what you want to do with that pointer

play09:26

you can do so that is a good practice

play09:29

before accessing before dereferencing

play09:31

any pointer in the program better to

play09:33

check if pointer is null or not if null

play09:35

we cannot dereference it

play09:36

otherwise your program is going to be

play09:39

crashed

play09:40

right

play09:41

so this is one reason

play09:43

why dangling pointer comes this is what

play09:45

you can say dear location of memory or

play09:48

you can say

play09:49

destruction of objects

play09:51

right so it means what you can say

play09:53

dangling pointer points to a

play09:56

pointing to a freed memory location

play10:00

right because

play10:01

this has been freed

play10:03

so a pointer which is pointing to a free

play10:05

memory location that is known as

play10:08

dangling pointer that's it

play10:10

right i hope you got the definition of

play10:12

dangling pointer now

play10:14

one more reason why a dangling pointer

play10:17

comes in your program is one reason is

play10:19

this

play10:20

right because you have dynamically freed

play10:22

the memory and pointers still exist

play10:25

right

play10:26

one reason is this and how to get rid of

play10:27

the situation you can point it to none

play10:30

second is what in main function

play10:33

i am taking a pointer

play10:35

right

play10:36

and

play10:37

here uh maybe you write down some code

play10:40

and within this code i am writing i am

play10:42

taking on block like again curly braces

play10:45

int a is equal to suppose 9

play10:47

and in within this block i am saying ptr

play10:50

is equal to address of a

play10:53

and within this block i am just printing

play10:55

a is equal to

play10:57

and i am accessing this a using ptr so

play11:01

here it will print 9

play11:03

so this is the block

play11:05

right where i am using this a variable

play11:07

so a is what now

play11:09

local variable to this block only

play11:11

you can say this is the scope of this a

play11:14

out of this scope if you will use this a

play11:17

then it will give error because it is

play11:19

local to this

play11:21

right

play11:22

here only you can say a would be created

play11:24

a is having nine and some address also

play11:26

suppose thousand

play11:28

and

play11:29

ptr is global

play11:31

not global but within this main function

play11:33

so

play11:34

throughout the main

play11:35

this program you can use ptr so in ptr

play11:38

now we have address of a that is

play11:40

thousand so ptr is now pointing to this

play11:43

a

play11:44

right

play11:44

and here if you print strict ptr 9 would

play11:46

be printed but

play11:48

after this block as soon as control will

play11:50

go out of this one

play11:52

this would be vanished

play11:54

if after this you will print printf

play11:57

like directly i am printing a

play12:01

you cannot access a here it will give

play12:02

you error

play12:03

you are accessing a local variable

play12:06

it will give you error

play12:07

right and after

play12:10

this also if you use like a strict ptr

play12:13

then also it it is acting as a dangling

play12:16

pointer now because uh out of this block

play12:19

now a has been vanished

play12:22

this there is no a now

play12:24

but this ptr is still pointing to this

play12:26

memory location but there is no nothing

play12:29

here

play12:30

that has been you can you can say

play12:31

vanished so this pointer is now it's

play12:34

like hanging

play12:35

so this will act as a dangling pointer

play12:38

out of this

play12:39

scope

play12:40

right

play12:41

yeah sometimes still

play12:43

if you run this still maybe for a while

play12:46

you can get 9

play12:48

maybe you are thinking that it's wrong

play12:50

but it's ok maybe for a while for some

play12:53

time you can get correct output nine it

play12:55

shouldn't give nine because now

play12:58

nine is no more here it has been

play12:59

vanished it should give any garbage but

play13:01

maybe you can get nine you can run it

play13:03

and you can check it but

play13:05

after some other function call after

play13:07

some time maybe this memory has been in

play13:10

stack

play13:11

instead

play13:13

because when we run a program then the

play13:15

some memory has been allocated

play13:17

right to main function obviously that

play13:20

would be copied to ram main memory so

play13:23

this for a also some memory has been

play13:25

allocated for ptr also some memory has

play13:27

been allocated for also memory has been

play13:29

allocated this is what within this scope

play13:32

only scope is this one out of this this

play13:34

would be vanished

play13:36

right

play13:37

so maybe for a while you can get nine

play13:39

same output

play13:41

but

play13:42

after some time after other function

play13:44

call after in your program

play13:46

this memory maybe will be allocated by

play13:49

the operating system to some other

play13:50

variable to some other function

play13:52

then if you will dereference it using

play13:54

this pointer it will give some garbage

play13:56

value

play13:57

right

play13:58

but yeah it will act out of the scope as

play14:01

a dangling pointer

play14:03

right

play14:03

third reason may be like

play14:07

one one

play14:09

before this main function before this

play14:10

main function i am taking one function

play14:12

like

play14:14

f and i am taking here

play14:18

a is equal to 10

play14:19

and here i am returning return address

play14:22

of a

play14:24

this function is returning address of

play14:26

this variable

play14:27

right and within main function within

play14:30

main function what i am writing

play14:32

so it is returning address

play14:35

so

play14:36

to catch address to store address of any

play14:38

variable we should have some pointer

play14:41

variable so here i am having pointer

play14:43

variable

play14:44

and whatever this function return f

play14:47

return

play14:48

that would be stored in this pointer

play14:50

right it is returning address so that is

play14:53

why it is int star

play14:55

string

play14:56

right

play14:57

so it will read an address and this

play14:58

pointer is going to

play15:00

store that address and within main

play15:02

function i'm writing like i'm

play15:05

dereferencing this as strict ptr like

play15:07

whatever the value at that address it

play15:10

should be printed like it should be

play15:11

printed 10 because it will get an

play15:13

address of a

play15:14

so in a we have 10 so it should print 10

play15:17

but

play15:18

here this ptr is acting as a dangling

play15:21

pointer why so same

play15:23

this a is within this function so scope

play15:26

of this a is within this function only

play15:28

right

play15:29

now it will return address of a

play15:32

but

play15:33

after returning as soon as control will

play15:35

go out of this function

play15:37

this a would be vanished it would be

play15:40

deleted

play15:42

so now

play15:44

here this pointer is obviously it has

play15:46

returned the address here

play15:48

right

play15:49

so in ptr we have address of a but as

play15:52

soon as control will go out of this

play15:55

function in the main function here

play15:57

here because i am calling function here

play16:01

one the control will go out of this one

play16:04

this would be vanished

play16:05

so now

play16:07

what this pointer is still hanging

play16:09

because whatever the address it is

play16:11

containing that is no more now that

play16:12

memory is now freed

play16:14

it has been vanished

play16:16

right so now also it is acting as a

play16:18

dangling pointer

play16:20

and if you will you know like

play16:21

de-reference it maybe it will give some

play16:23

garbage value to you and see here

play16:26

if because this scope of this is within

play16:29

this function only but if you declare it

play16:31

static

play16:33

static variables are having scope but

play16:36

global globally means throughout the

play16:38

program

play16:39

so if you declare it

play16:42

static

play16:43

then it is not going to vanish it will

play16:46

return address of this a and if in main

play16:49

function if you will dereference it it

play16:50

will give you 10.

play16:52

now it's fine now it is not acting as

play16:54

dangling pointer right because we have

play16:56

used static here this also will discuss

play16:59

in detail but for

play17:01

this video you have to take you know how

play17:03

to understand this thing

play17:04

static variables these are having

play17:07

you know scope you can say globally or

play17:09

throughout the program

play17:11

right

play17:12

so now let me show you uh you know

play17:13

practically what is dangling pointer so

play17:16

now let me create a file

play17:31

header file we use and uh

play17:34

we also use one more header file that is

play17:37

stdlib

play17:40

because here we use like for that

play17:44

malloc and malloc function dynamic

play17:45

memory allocation and that is built-in

play17:47

function that has been i know

play17:49

uh defined in this header file stdlib

play17:52

right

play17:56

so first of all i am taking like uh that

play17:59

same

play18:00

one pointer

play18:02

and dynamically

play18:03

what

play18:05

we are allocating memory

play18:07

using

play18:08

malloc so

play18:10

size of

play18:16

and we have allocated like the memory so

play18:19

that

play18:20

in that memory i am storing

play18:22

10

play18:23

and i am printing

play18:27

percentage d

play18:29

i'm accessing that thing using

play18:31

that memory using pointer only

play18:33

right

play18:36

so i guess it should print what 10

play18:39

if the syntax is correct so let's see

play18:42

what happens

play18:44

here

play18:50

say it is giving output 10

play18:53

right

play18:54

so now

play18:55

what i'm doing

play18:57

now free the memory

play19:01

whatever the memory pointer is pointing

play19:03

free that

play19:04

this is the syntax of this free function

play19:07

free and just you have to pass the

play19:09

pointer which is pointing to that memory

play19:11

location so that memory has been free

play19:13

like vanished

play19:15

but pointer is still pointing to that so

play19:17

now pointer is acting as a dangling

play19:19

pointer so now if you will access this

play19:21

like printf

play19:24

again here i am using

play19:26

a strict ptr

play19:29

what it should give

play19:31

you don't know maybe it will give some

play19:33

garbage value

play19:34

see

play19:35

10 first time and after that it is

play19:37

giving some garbage value

play19:40

right and suppose again

play19:42

run this at this time again

play19:45

some another value garbage value right

play19:48

so this is what dangling pointer so how

play19:50

you can get rid of the situation

play19:52

after freeing directly you know simply

play19:55

do

play19:56

ptr is equal to null

play19:58

and you cannot dereference null pointer

play20:00

so please don't dereference null pointer

play20:02

now before dereferencing in a program

play20:04

good practice is what use sorry check

play20:07

if this is null pointer is null you

play20:09

cannot dereference it else you can do

play20:12

whatever you want to do with that

play20:13

pointer so one situation is this

play20:16

next can be

play20:18

next can be like i am just going to

play20:20

comment this out

play20:22

right

play20:27

and next we are using that

play20:29

function

play20:31

right so

play20:33

before using this main function what i

play20:35

am defining a function f

play20:40

and here i am taking a variable int is

play20:42

equal to 9 and

play20:45

return what address of a

play20:49

that's it and in main function what

play20:51

we will take a pointer because

play20:54

to store address obviously we need a

play20:56

pointer variable

play20:58

so whatever this function will return

play20:59

this pointer is going to take right and

play21:03

after that

play21:05

we are printing

play21:09

i'm writing this line like printf

play21:11

percentage d

play21:13

and strict ptr so i'm accessing this ptr

play21:16

now

play21:17

right

play21:18

so let's see what happens let me run

play21:20

this

play21:23

see

play21:25

it is not returning anything like no

play21:27

output you are getting and the function

play21:29

returns address of a local variable

play21:32

address of a is local variable right so

play21:35

here this ptr is acting as a dangling

play21:36

pointer you are not getting any output

play21:38

right

play21:40

maybe sometimes it will give any garbage

play21:42

value it

play21:43

we cannot say it will show some

play21:45

unpredictable behavior

play21:47

right

play21:48

and

play21:49

next is what

play21:53

if i am taking a pointer here

play21:55

right suppose pointer is equal to so

play21:58

better to initialize

play22:00

a pointer at starting when you take a

play22:01

pointer better to initialize it with the

play22:03

null

play22:04

so if you are not initializing here with

play22:06

any valid address so better to

play22:07

initialize it with

play22:08

null so next case is uh i'm not taking

play22:12

this function now i'm deleting it

play22:16

so pti is equal to null and here i am

play22:18

taking

play22:20

like

play22:21

within this block i am taking a variable

play22:23

a is equal to five 5

play22:25

and

play22:26

here ptr is equal to address of

play22:30

a

play22:31

and i am printing printf

play22:33

a is equal to whatever the value is

play22:36

having i'm accessing this using

play22:38

ptr so a strict media

play22:41

right

play22:43

suppose this line i'm going to comment

play22:44

it out right so this is what we have see

play22:48

this is what we have

play22:51

right so what output you will get

play22:53

obviously you will get 5

play22:55

right

play23:00

see a is equal to 5

play23:02

but ah

play23:03

out of this

play23:05

block this is the scope of the say if

play23:07

out of this using this printf statement

play23:11

out of this block i am accessing

play23:13

strict ptr

play23:15

now here it is acting as dangling

play23:17

pointer

play23:18

so it will show some unpredictable

play23:20

behavior maybe a program will crash or

play23:21

it will give some bug or

play23:23

it can it can create some bug or it will

play23:25

give some garbage value we cannot say

play23:27

but sometimes it will give the same

play23:30

value that is 5 it is also possible see

play23:33

let me run this and see what happens

play23:41

okay what

play23:43

just a minute

play23:46

now let me run this

play23:49

see a is equal to 5

play23:51

for this a is equal to 5 and this printf

play23:54

is also giving you 5

play23:56

but although the scope of this a is

play23:58

within this only it has been vanished

play24:00

but still it is giving you 5.

play24:03

it is also possible for a while it this

play24:05

will give you correct output

play24:07

but after some time when you write some

play24:10

other in your program maybe that you

play24:12

know freed memory location would be

play24:13

allocated to some other function or some

play24:15

other variable

play24:17

so after that this this pointer this

play24:19

line this pointer will show

play24:21

it will not give you correct output it

play24:22

will show some unpredictable behavior

play24:25

right so this is also fine maybe you are

play24:27

thinking it is giving 5 so it is i know

play24:30

it is not correct but no yeah it can

play24:32

give you some you know correct result

play24:34

for uh for some time for a while you can

play24:37

say

play24:39

so that's it for this video now so i

play24:41

hope you got what is dangling pointer

play24:43

and see when we are left with one more

play24:44

pointer that is wild point tennessee so

play24:46

now that thing we'll discuss in next

play24:47

video so i'll see in the next video till

play24:49

then bye take care

Rate This

5.0 / 5 (0 votes)

Ähnliche Tags
C ProgrammingDangling PointersMemory ManagementCoding TutorialGATE PrepDynamic AllocationProgramming ErrorsCode ExamplesFree MemoryNull Pointer
Benötigen Sie eine Zusammenfassung auf Englisch?