Difference between null pointer and dangling pointer?

Answers were Sorted based on User's Feedback



Difference between null pointer and dangling pointer?..

Answer / vrushali

NULL pointer points to nothing.
But dangling pointers are those pointers which points to
invalid location (e.g. still points to those memory
locations which are already freed)

int *p = NULL;
Is mere a pointer which is not initialised to valid
memroy location. it points to nothing

int *q ;
q -> 0x1A
free (q);

still we can access this pointer using q. Still memory has
not been released to the system.

Is This Answer Correct ?    27 Yes 5 No

Difference between null pointer and dangling pointer?..

Answer / rakshitha

Null pointer is a pointer which doesnot point to any where
in the memory location ,where as dangling pointer points to
memory location having garbage value.

Is This Answer Correct ?    8 Yes 1 No

Difference between null pointer and dangling pointer?..

Answer / vrushali

free should be able to free as it maintains a table from
where it can start freeing the memory. It is OS related issue.

There is no way in C where we can ensure that the particular
memory is freed a even after free we can access the variable
for some time. In Java, we have a memory deallocator.

Is This Answer Correct ?    5 Yes 1 No

Difference between null pointer and dangling pointer?..

Answer / vadivel t

To Sourisengupta Question.

free() shall free the memory which is allocated dynamically.

But afrer the free() function being called,the pointer
which u r passing to free() as an argument, shall point to
the same base address, which is no more valid.

Is This Answer Correct ?    2 Yes 0 No

Difference between null pointer and dangling pointer?..

Answer / vadivel t

Minor correction in my ans #4...

To Sourisengupta Question.

free() shall free the memory which is allocated dynamically.

But afrer the free() function being called,the pointer
which u r passing to free() as an argument, shall point to
the same base address, which is no more valid(ie., address
is valid but no more in the allocated memory pool. It will
be added in the free memory pool).

Is This Answer Correct ?    2 Yes 1 No

Difference between null pointer and dangling pointer?..

Answer / ravi

Both are very different.
NULL macro is
#define NULL 0
it means the macro NULL will be replaced by 0 while
preprocessing
But the NULL pointer means it points to nowhere i.e. contains 0.
It contains 0 means it may be dangerous to use such pointer
without assigning proper address to it otherwise NULL
pointer may try to access reset address may cause the
program to crash.

Is This Answer Correct ?    0 Yes 0 No

Difference between null pointer and dangling pointer?..

Answer / sourisengupta

If free() is not able to free the memory then how we are
freeing the memory????

Is This Answer Correct ?    4 Yes 6 No

Post New Answer

More C Interview Questions

What do you mean by team??

5 Answers   Student,


develop a breakfast order booking system using Functions, Structures and Arrays. The system is to support the operations shown to the user in the following system’s menu: Welcome to Asim's Restaurant Select an operation? 1 Make a breakfast order 2 Modify existing order 3 Cancel existing order 4 Print Bill 5 Exit Type in your selection (1, 2, 3, 4 or 5): The program will include an array of structures. The structure type is called MenuItem, and the array of structures should be declared as menu[50] to store upto 50 breakfast menu items. The MenuItem structure must have the following 4 data fields (i.e. members): Code, description, unitprice, and quantity. Your program must define at least the following functions:  Function LoadData(…) that loads the data of breakfast menu items from an input file menu.txt into the array menu.  Five separate functions to handle the program main menu five tasks shown above. In general, function main() should load the data into the array menu and display the main menu of the above mentioned tasks. Based on the option selected, the corresponding function should be called. Users must make an order before requesting modify, cancel or print order bill (See the given sample run). Here is a more detailed explanation on the above tasks and functions. LoadData(…) Opens the input file menu.txt (you will create it) containing breakfast menu items data (See below) and assign these values to the corresponding array menu structure fields. Assign zero to the quantity field of the loaded menu items. Welcome to Asim's Restaurant Select an operation? 1 Make a breakfast order 2 Modify existing order 3 Cancel existing order 4 Print Bill 5 Exit Type in your selection (1, 2, 3, 4 or 5): MakeOrder(…) This function is called when the user selects option 1 from the main menu. It first displays the breakfast menu items to the user (stored in the array menu) along with all the necessary information (item code, description and unit price). It then requests the user to select an item using the item code (see the sample run). When the user enters an item code number the program should verify the code number and increment the quantity of the selected item in the array menu. Your program should reject invalid item codes and request the user to re-enter the item code or stop the entry. PrintBill(…) This function is called when the user selects option 4 from the main menu. The function displays the breakfast menu items in the breakfast order by printing the values of all MenuItem members of the array menu that have quantity value above zero. Print a nicely formatted bill (See the sample run). CancelOrder(…) This function is called when the user selects option 3 from the main menu. It will cancel the breakfast order by setting the quantity value of all the array menu structures to zero. ModifyOrder(….) This function is called when the user selects option 2 from the main menu. The task of this function is to allow the user to edit an existing breakfast order with two operations: Adding more breakfast items and/or altering the breakfast item that were ordered before (examine the sample program run). Handling Errors and Invalid Input Your program should reply safely to any erroneous situation or input with appropriate message to the user and flow nicely and correctly to the next operation (Examine the sample program run). Additional functions Use of functions is highly recommended for any additional special tasks needed by your solution. Global variables are not allowed except for max array menu size 50. You must pass the needed parameters through functions parameter lists.

0 Answers  


How can I call a function, given its name as a string?

4 Answers   ABC Telecom,


main() { intj; while9j<=10) { printf("\n%d",j); j=j+1; } }

4 Answers  


What is the difference between i++ and i+1 ?(in terms of memory)

3 Answers   HCL,


how to execute a program using if else condition and the output should enter number and the number is odd only...

0 Answers  


what is used instead of pointers in java than c?

1 Answers   Vuram,


how memory store byte

4 Answers   Huawei,


If fflush wont work, what can I use to flush input?

0 Answers  


In C programming, what command or code can be used to determine if a number of odd or even?

0 Answers  


Explain what are the different data types in c?

0 Answers  


6. Which of the Following is not defined in string.h? A)strspn() B)strerror() C)memchr() D)strod()

2 Answers   Accenture,


Categories