UrbanPro
true

Take BTech Tuition from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Some Interview Questions and Answers for fresher level on Pointers

Adroit Learning Foundation
26/09/2016 0 0
What is a void pointer? Void pointer is a special type of pointer which can reference or point to any data type. This is why it is also called as Generic Pointer. As a void pointer can point to any data type, neither it is possible to dereference a void pointer, nor is it possible to do any arithmetic operations on void pointer. Hence an explicit type casting is absolutely must for using a void pointer.
The best use of void pointer can be exhibited in the case of malloc and calloc.
What is a null pointer? A null pointer is also a special type of pointer which points definitively to nothing.As per official C language description, every valid type of pointer has a special value called “Null Pointer” and this value is always distinguishable from all other pointer values and it is guaranteed to compare unequal to a pointer of any object or function.
Hold it for a second! So what you are saying is there are different versions of null pointers for every pointer type. Is it so? No. We cannot call it as a “different versions” rather their internal values are different. But as a programmer, we need not to worry about it because compilers take care of and track the internal values (may be differently depending on the compiler types)
So why don’t you say that a null pointer is a pointer which is not initialized yet? I cannot go with this idea because an uninitialized pointer may point to any thing. There might be an uninitialized char pointer, an uninitialized int pointer and so on. But a null pointer is definitely something which is there not to point anything.
Ok. Now tell me what is a dangling pointer? Dangling pointers are the pointers which point to already de allocated or de referenced memory locations. Suppose I write a piece of code like this int*p;p = (int*)malloc(n*sizeof(int);--------free(p); In this piece of code, after allocating some memory to p, we are de allocating the memory which was initially pointed by p. Hence now onward, p acts as dangling pointer.
How to rectify this? In this case, after de-allocation that is after using free() we should set the pointer p to null as p = NULL
Could you please exhibit any other source of dangling pointer problems? Well, there might be other sources also, like, Suppose I have declared int*p, and within a specific scope, I have defined another integer x as 10; and then within that specific scope I am making the pointer p to hold the base address of x. Now outside this specific scope the p will act as a dangling pointer.
I can write the code as
void foo (void)
{
int *p;
{
{
int x = 10; p = &x;
} // here p will act as dangling pointer
}
} Another case might occur when from a function, we return an address of any variable and a corresponding pointer in the receiver section receives it, then the receiving pointer might get into dangling pointer problem, as just after the returning, as the variable has passed it scope, the memory stack (where the variable is stored currently) might be refreshed. But this phenomenon is not obvious, this might happen.
Fine, so how to deal with this second case? To deal with this, to be in safer side, we must have the scope of the variable intact out of the function call; declaring it as static might be an option.
0 Dislike
Follow 0

Please Enter a comment

Submit

Other Lessons for You

Tips for Entry Level Java Job Seeker
3 steps you should take to make a break as a Java developer: Step 1: Invest in improving your job hunting skills encompassing interviewing, networking, and resume writing skills. Step 2: Keep applying...
A

Akshay Shende

1 0
0

Preparation for exams
Dear Engineering students, Examination time has come for Engineering students. 1. You find how much time left for final exams in this semester or year. 2. Plan the preparation according to the time. 3....

Living with Concepts
Always it is a complain from many students that they do not remember concepts taught and learnt as time progresses. Even exceptional students also falls pray to effect of time fading. The tip which i generally...
B

Babu T

0 0
0

Drag And Its Classifications
Introduction Drag is the enemy of flight and its cost. One group of those forces is aerodynamic forces that split into two forces: Lift force or lift, and Drag force or drag. A pre-requisite to aircraft...

E-magazine
E-magazine is a magazine published on the World Wide Web. Some online magazines may refer to themselves as electronic magazines or e- magazine to reflect their readership, demographics or to capture alternative...
X

Looking for BTech Tuition Classes?

The best tutors for BTech Tuition Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Take BTech Tuition with the Best Tutors

The best Tutors for BTech Tuition Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more