C_81 Null Pointer in C | C Programming Tutorials
Summary
TLDRThis video script discusses null pointers in C programming, explaining their definition, usage, and importance. It differentiates null pointers from void and uninitialized pointers, emphasizing the risks of dereferencing a null pointer and the benefits of initializing pointers with null for error handling. Practical examples are provided to illustrate the concepts.
Takeaways
- 😀 A null pointer in C is a special pointer that does not point to any valid object or memory location.
- 🔍 Null pointers are typically initialized with the value `NULL` (which is zero in C), ensuring they do not point to any valid memory address.
- 🚫 It is crucial not to dereference a null pointer, as doing so will lead to undefined behavior and potentially crash the program.
- 💡 The primary use of null pointers is for error handling, especially in scenarios where dynamic memory allocation might fail.
- 📚 Null pointers are defined in standard C header files like `<stdio.h>` and `<stddef.h>`, making them a standard part of the language.
- 🔄 When a pointer is not initialized, it contains a garbage value, making it a wild pointer, which is risky to use without proper checks.
- 🔑 Initializing a pointer with `NULL` is safer than leaving it uninitialized, as it avoids undefined behavior and potential crashes.
- 🔍 Before performing operations on a pointer, it is essential to check if it is null to prevent dereferencing a null pointer.
- 🔗 Null pointers are different from void pointers, which are pointers that can point to any type of data, while null pointers specifically do not point to any valid object.
- 🔄 Comparing two null pointers will always result in equality because they both contain the value zero, indicating they do not point to any valid memory location.
Q & A
What is a null pointer?
-A null pointer is a special pointer in C that does not refer to any valid object or address. It is initialized with the value zero. In the context of pointers, null is a macro defined in the standard library headers that represents a null pointer constant.
Why is it important to initialize a pointer with null?
-Initializing a pointer with null is important to avoid undefined behavior. An uninitialized pointer can contain garbage values, which may point to any random memory location, potentially leading to crashes or incorrect program behavior. Using null ensures that the pointer does not accidentally dereference an invalid memory address.
What happens if you dereference a null pointer?
-Dereferencing a null pointer will result in a program crash because a null pointer does not point to any valid memory location. Attempting to access the memory location it points to (which is zero) will lead to undefined behavior, typically causing the program to terminate.
How is a null pointer different from an uninitialized pointer?
-A null pointer is explicitly initialized to zero and is a valid pointer constant that does not point to any object. An uninitialized pointer, on the other hand, has not been assigned any value and may contain a garbage value, which is unpredictable and could point to any random memory location.
What is the purpose of using a null pointer in programming?
-The primary purpose of using a null pointer is for error handling and to indicate that a pointer does not point to a valid memory location. It is commonly used in scenarios where memory allocation might fail, or when a function needs to indicate that no valid address is being passed.
Can you compare two null pointers for equality?
-Yes, you can compare two null pointers for equality. Since both null pointers are initialized to zero, comparing them will always result in true, indicating that they are equal.
What is the difference between a null pointer and a void pointer?
-A null pointer is a pointer that does not point to any valid object and is initialized to zero. A void pointer, on the other hand, is a generic pointer type that can point to any type of data. The null pointer is a specific value that indicates no valid address, whereas a void pointer is a type that can be used to point to any data type.
How can you check if a pointer is null before dereferencing it?
-You can check if a pointer is null by using an if statement to compare the pointer to null. For example, `if (ptr == NULL) { /* pointer is null, handle accordingly */ }`. This check helps prevent dereferencing a null pointer, which would lead to a program crash.
Why is it risky to use an uninitialized pointer?
-Using an uninitialized pointer is risky because it may contain a garbage value that points to an arbitrary memory location. Dereferencing such a pointer can lead to undefined behavior, including program crashes or corruption of data, as the program might access or modify memory that it should not.
How does a null pointer affect dynamic memory allocation?
-In the context of dynamic memory allocation, a null pointer is used to indicate that memory allocation has failed. If a function like `malloc` or `calloc` cannot allocate the requested memory, it returns a null pointer. Checking for a null pointer after a memory allocation attempt is crucial to handle such failures gracefully.
Outlines
📌 Introduction to Null Pointers
This paragraph introduces the concept of null pointers in programming. It explains that null pointers are special pointers that do not point to any valid memory location. The speaker discusses the importance of understanding null pointers, as they are often a topic in interviews and are crucial for avoiding undefined behavior in programs. The paragraph also touches on the difference between uninitialized pointers, which can point to any random memory location, and null pointers, which are explicitly set to point to no location. The speaker emphasizes the need to initialize pointers to null to prevent potential crashes or errors in the program.
🚫 Avoiding Dereferencing Null Pointers
The speaker continues the discussion on null pointers, focusing on the dangers of dereferencing them. It is highlighted that dereferencing a null pointer will lead to a program crash because null pointers do not point to any valid object. The paragraph also covers the initialization of pointers with null values, explaining that this practice is safer than leaving pointers uninitialized. The speaker provides an example of how to check if a pointer is null before dereferencing it, which is a common practice in error handling in C. The importance of this check is underscored by the potential for memory allocation failures, which could result in a pointer remaining null.
🔍 Uses and Purposes of Null Pointers
In this paragraph, the speaker delves into the specific uses and purposes of null pointers in programming. It is explained that null pointers are often used in data structures and during dynamic memory allocation to indicate that a pointer does not currently point to a valid address. The speaker also mentions that null pointers can be used to pass no valid address to a function, which will be discussed in more detail in future videos. The paragraph emphasizes the importance of initializing pointers with null to avoid undefined behavior and the need to check pointers before using them to ensure they are not null.
🔬 Practical Demonstration of Null Pointers
The speaker provides a practical demonstration of null pointers in this paragraph. They show how to declare and initialize pointers in C, and how to compare them. The demonstration includes printing the values pointed to by different pointers, including a null pointer and an uninitialized pointer. The speaker also illustrates the consequences of attempting to dereference a null pointer, which results in a program crash. The practical example serves to reinforce the theoretical concepts discussed earlier in the video.
🏁 Conclusion and Upcoming Topics
In the final paragraph, the speaker concludes the discussion on null pointers and provides a brief overview of what will be covered in the next video. They summarize the key points about null pointers, emphasizing that they do not refer to any valid object and should not be dereferenced. The speaker also invites viewers to ask questions in the comments if they have any doubts. The next topic to be discussed is the concept of dangling pointers, which will be explored in the subsequent video.
Mindmap
Keywords
💡Pointers
💡Null Pointer
💡Void Pointer
💡Uninitialized Pointer
💡Dangling Pointer
💡Dynamic Memory Allocation
💡Dereference
💡Error Handling
💡Garbage Value
💡Header Files
💡Memory Address
Highlights
Introduction to null pointers and their importance in programming.
Difference between uninitialized pointers and null pointers.
Explanation of what a null pointer is and why it is used.
Null pointer is a special pointer in C with a value of zero.
Null pointers are used to prevent undefined behavior caused by uninitialized pointers.
Null pointers are used in error handling in C.
Null pointers are used when you don't want to pass any valid address in functions.
Null pointers are initialized with 'null' or '0', which is defined in C header files.
Null pointers point to a reserved memory location that cannot be dereferenced.
Practical demonstration of initializing pointers with null.
Difference between null pointers and void pointers.
Explanation of why you cannot dereference a null pointer.
Demonstration of the consequences of dereferencing a null pointer.
Importance of checking if a pointer is null before dereferencing.
Discussion on the use of null pointers in dynamic memory allocation.
Practical example of checking if a pointer is null before accessing its value.
Difference between uninitialized pointers and null pointers in terms of memory address.
Conclusion and summary of the key points about null pointers.
Transcripts
so in the series of learning programming
in see we are discussing pointers in say
in the previous video I have discussed
void pointer in say in this video I'll
talk about null pointer in because there
are some special pointer in see also
like void null wild pointer and dangling
pointer these these concepts are also
really very helpful and generally in
interview also they ask these type of
questions right so we'll see everything
about null pointer in this video like
what is null pointer why we use it that
is very important the application of
null pointer all the important points
about null pointer we will see with the
help of a proper program we'll discuss
here first of all then I'll show you
practically on my laptop and we'll also
see how this pointer is different from
void pointer as well as if you don't
initialize any pointer right how this
pointer is different from that pointer
that that an uninitialized pointer right
everything about null pointer we will
discuss in this video so now let's see
what is null pointer see I hope you know
uh how to initialize how to declare a
pointer suppose I'm wenting int star
PTR this is this is what a pointer to
integer you can say an integer pointer
right here we are not initializing this
pointer
means here is some
PTR in memory right it's a special
variable it will contain address of any
other variable but we are not assigning
any address till now right so maybe it
is pointing to some some address in
memory which is which is
garbage because here we are having some
garbage
value we are not initializing it so it
is pointing to any memory location right
we don't know the address of that memory
location so that at that memory location
maybe this memory location is using by
operating system itself for some
important task or maybe some other
process is using this memory location
right or maybe this memory location is
not valid at
all anything can be possible so this is
what uninitialized pointer we are not
initializing this pointer it is
containing any garbage value we don't
know to which memory location is it is
pointing that memory location may be
valid may not be valid so when you use
this pointer when you D reference now
this pointer like print F percentage D
and I want to De reference it a
PTR by mistake I haven't initialized it
but by mistake in program I want to find
out the value that is pointed by this
pointer I want to Der reference means
value at this address
but address in this pointer is any
garbage value because we haven't
initialized this yet so what you will
get here we we cannot say it will show
some undefined
Behavior maybe program will crash or
maybe it will give some garbage value we
don't know it
depends right so it will show some
undefined Behavior so to you know remove
this thing generally if you you don't
initialize the pointer that is known as
wild pointer so these are risky to use
right so better to initialize it with
null this null all caps letters n u l
this is itself a pointer in C and the
value is zero the corresponding value of
this null is zero this zero is so in C
you can write down null or
zero but this zero we are using in
context of pointers because this is
pointer it is different from that
integer zero right so better to use here
null rather than zero n u l ultimately
it would be zero so if you print here
now if you print here like PTR in PTR
now we have what null that is zero right
it is already defined right in those
Hile stdi in STD f.h in STD li. multiple
header files it it has been defined
right and its value is corresponding
value of N is a zero so in pointer now
we have
zero right so it is pointing
to to what because memory location is
zero so you can say it is a special
value that is pointing to that does not
point to any valid object or you can say
it is pointing to zeroth memory location
which is reserved which is reserved you
cannot dereference that memory
location this thing is very important
you cannot dreference
it right or you can say it is pointing
to it it doesn't point to any valid
object any valid
address if you write here if you write
this s p is equal to none right so if
you print here PTR it will give zero and
if you try to dreference
it your program will
crash this thing is very important you
cannot dereference a null pointer so see
this null n this is itself a pointer in
C
right the value of this is corresponding
value of this this is what zero this is
null pointer in see and we can also use
this pointer to initialize another
pointer variable because PTR is a
pointer variable I'm declaring a pointer
of type int right and I'm initializing
this with this pointer null pointer so
now PTR also becomes a null pointer
right so in simple terms you can say
null pointer null pointer does not refer
to any valid address or it does not
refer to any valid
object so if this is the case then what
is the purpose of using null
pointer there are some special purpose
because that is why we use null pointer
in our programming language and what are
those purpose
see if by
chance I'm not initializing the pointer
right now right because I want this
pointer should point to a memory
location which I would allocate
dynamically using Malo and Cog function
right suppose I'm uh using this pointer
is equal
to I'm using Malo function and I'm using
here five into size of
int dynamically at run time I want to
allocate memory these Malo and Cog these
functions are built-in functions which
are used to allocate memory dynamically
this thing we have discussed in data
structure videos that you can you can
watch that video videos or we'll see
this thing later right if you you you
are not getting this syntax leave it the
the the meaning of this is simple
meaning of this is what here I'm
allocating some memory right and that
memory this pointer should
contain memory address that memory
address right so suppose at run time I'm
allocating this memory and address of
this is th000 so it means this th000
should store here so now this PTR is
pointing to this memory address now this
is valid memory address right but in
starting I'm not initializing this poter
at run time I want to initialize but by
chance because of some
problems because of some problems it is
not possible to allocate memory using
myow it is not possible to allocate
memory during this time so ultimately in
pointer in PTR null would be assigned
null means zero would be
assigned right so now at this point we
think that we have written this syntax
and memory has been allocated to this
PTR and now this pointer is pointing to
any valid memory location and further in
the program I'm using something like I'm
I'm supposed de referencing this pointer
estri
PTR right whatever here I I want to
access this right but because of some
problems this Malo has not been executed
successfully and the memory has not been
allocated to this this PTR the memory
has not been allocated so now in PTR by
default we have
null and if you now dreference this
pointer it will show
what your program will crash because we
cannot dereference a null
pointer right so better to before before
de referencing this better to check
if PTR equal to equal to
null if PTR is null you have to be very
careful you cannot de reference that
pointer or you cannot do anything
because pointer is this pointer is
pointing to invalid memory object
invalid object right so we cannot do
anything just you can
print what memory has not been allocated
else whatever you want to do you can do
if memory has been allocated
successfully right so now at starting
please don't leave the pointer
uninitialized better to initialize it
with null so ultimately at starting we
have null in this pointer and now if
memory has been allocated successfully
it's okay maybe the memory address is
th000 so here in PTR now we have th000
and it is pointing to this memory
address right so better to check now
before doing something with this pointer
better to check if pointer is still null
memory has not been allocated if not n
null then you can do something with this
pointer right so it is what you can say
it is used in error handling in
C because by mistake if pointer is not
null like you haven't initialized this
pointer and maybe by mistake you want to
deference that pointer in the program so
your program will
crash right so to get rid of the
situation to handle this error we use
null pointer we initialize it with null
this is the main thing this is the main
purpose of using null pointer many times
in data structure we use null pointers
you can watch out my data structure
playlist there I have used this null
this keyword null this null pointer many
times generally we use uh when we use
dynamic memory allocation in that case
we use null pointers that is the main
purpose of using null pointer right and
also one more thing like in function if
you don't want to pass any valid address
suppose I'm using any function and here
uh at sometimes we don't I don't want to
pass any valid address so you can pass
null pointer here right so that we'll
discuss when we will discuss the this
function concept at that time we'll
discuss this thing right so in brief
what we can say about null pointer is
what null pointer does not refer to any
valid object
secondly better to initialize a pointer
with null rather than leaving it
uninitialized right to get rid of the
situation of that undefined Behavior
right so better to initialize it with
null and before doing something with
this pointer better to check if pointer
is null don't do anything because the
third point is very important third
point is very important you cannot
dreference a null pointer because it is
not pointing to any valid object so if
you will dreference it program will
crash right I'll show you practical also
of this null pointer and this is
different from void pointer as well as
uninitialized pointer this also you need
to take care of because null is what a
value
zero right although in the pointer
context and vo is what a type so both
pointer are different and one more thing
if you declare something like this int
pointer this pointer 1 is is equal to
null and ptr2 is equal to null
definitely these pointers would be same
right if you compare these pointer would
be same if you compare like if ptr1
equal to equal to PTR 2 and here you
will print both pointer P1 and uh
pointer ptr1 and ptr2 are same this
would definitely be executed this
because this would always be same
because in both we are storing null null
means zero so both pointer are pointing
to that zero memory location or you can
say any that is reserved right we cannot
deference it fine so both would be same
but if you write something like this in
ptr1 and if you don't initialize this
and
this so maybe you think you are not
initializing these pointer so these
pointer are also same no ptr1 now will
contain any garbage value PTR
2 will also contain some garbage value
but it is not guaranteed that this
garbage value and this garbage value are
same but if you initialize with null
that is for sure both will contain zero
and zero null and
null right so I hope you got this point
uh this
about the null pointer and uninitialized
pointer and if you declare something
like this in ptr1 is equal to null so in
ptr1 we have null that is zero right so
it is guaranteed that this is not this
value is not equal to any other pointer
which is pointing to any valid object
right because this will this is
containing zero that is
null right if you take any other pointer
this ptr2 this is uninitialized maybe
this will contain sometimes zero I we
cannot say we cannot say it depends or
maybe it will contain some other garbage
value but this is what obviously this is
what invalid
pointer uninitialized pointer this is
what invalid pointer right and if here
I'm storing address of like a I'm taking
a variable int a is equal to 5 now this
is what obviously a valid pointer
because it is it is containing address
of a is a variable somewhere in memory
right so this null pointer it is
guaranteed that it is not equal to any
other pointer which is pointing to any
valid
object right so now let me show you this
thing practically let me create a
file we use header files then main
function and here I'm declaring a
pointer uh suppose I'm I'm
taking
ptr1 right and uh see uh if I take a
variable in a is equal to suppose 3 and
ptr1 is equal to address of
a simply what you can
do percentage D and you can print you
can do what EST
ptr1 so it should print value of a that
is
three see it is printing
three right now if I write
here only a strict PTR I'm not going to
initialize
it and now D reference it or suppose I'm
I'm initializing this with
null and
L and one more poter I'm taking
ptr2 and that is uninitialized pointer
so whatever in ptr1 I'm going to print
that thing and one
more whatever value in ptr2 that also we
are going to print so in PTR 1 obviously
we will have see we are not using D
referencing operator we are printing the
value so in ptr1 we have zero and ptr2
we have any garbage Valu because it is
wild pointer uninitialized
pointer maybe now I have told you see in
first obviously we will have zero in
ptr2 also it is printing zero it may be
a case that it will have zero or it will
have any garbage value we cannot say but
null pointer will always have
zero right so ptr1 will always have zero
right now suppose uh here I'm uh storing
what
suppose I want to deference this null
pointer
see this is null pointer it is not
pointing to any valid object and still I
want to dereference it so obviously it
program is going to crash so
see okay so now
see see n pointer exe has stopped
working right because we cannot
dreference a null pointer this you need
to take care so you have to check before
D referencing something like this
if PTR equal to equal to what
null then what you will print here you
can simply
[Music]
write this
is null pointer right means you cannot D
reference it in lse part would you can
say now you can D reference it right so
now if you will run this what output you
will
get okay it's PTR it's not PTR it's PTR
1
ptr1 right so now let me run this and
see this is null pointer because
obviously this is null
pointer right so better to use this one
this we use uh for error handling in C
right and if you take another pointer
ptr2 is equal
to
null say here I'm taking two pointer
pointer two is also null and here I'm
checking if prtr 1 equal to equal to
ptr2
so this you have to print
like both are null
pointer otherwise you can do anything
what you want right so obviously both
are null so I have told you it is
guaranteed that both would always be
null so both are null pointer but if you
leave it uninitialized like PTR 1 also
and this is ptr2 I'm not going to
initialize it and now I'm going to check
right both are
uninitialized otherwise we can do
something in else part we can do
something right so now let me run this I
don't know what output we will
get see it has stopped
working why so because ptr1 is having
any garbage value ptr2 is also having
any garbage value we are not
initializing these point so it is it is
not a guarantee that that garbage value
would be same as the garbage value in
ptr2 right so if both are same then only
it will print both are uninitialized if
both are not same just in this case
means in lse part I'm going to print I'm
going to deference ptr1 and ptr1 is what
we haven't initialized it and I want to
print the value whatever the value
whatever the address in ptr1 at that
address what is the value but the
address is any garbage value so maybe it
will print anything or see it will show
some un undefined Behavior so here in
this case it has stopped working program
has been crashed
right so this can also be a
case so uninitialized pointer is
different from null pointer I hope now
you got the
difference and here also you can write
zero that is also fine ptr1 is equal to
zero and ptr2 also if I'm saying zero
and if both are same then it should
print both are null or zero so 0 0 is
also what is considered as null in U
C so here what it will
print see both are
zero right you can also write zero but
better to write down null not zero right
so I hope you got what is null pointer
if you have any doubt you can ask me in
comment box so in the next video we'll
see dangling pointer so now I'll see you
in the next video till then bye-bye take
care
Weitere ähnliche Videos ansehen
5.0 / 5 (0 votes)