Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

20. What is dangling pointer and how to avoid it?

Ans: After a call to free(p) makes a subsequent reference to *p illegal, i.e. though the storage to p is freed but the value of p(address) remain unchanged .so the object at that address may be used as the value of *p (i.e. there is no way to detect the illegality).Here p is called dangling pointer. To avoid this it is better to set p to NULL after executing free(p).The null pointer value doesnt reference a storage location it is a pointer that doesnt point to anything. ---------------------------------------------------

how to find the loop in singly linked list


if we are having the singly linked list then last node of the list is linked with middle (or) any node in the list then it causes the loop then how to find the loop with less time complexity Method 1.check the node pointed to by the outer loop, with every node of the inner loop. Method2. Have a visited flag in each node of the linked list. Flag it as visited when you reach the node. When you reach a node and the flag is already flagged as visited, then you know there is a loop in the linked list. Method3. Have 2 pointers to start of the linked list. Increment one pointer by 1 node and the other by 2 nodes. If there's a loop, the 2nd pointer will meet the 1st pointer somewhere. If it does, then you know there's one. Code for method 3 is as follows:
Code 1. p=head; 2. q=head->next; 3. while(p!=NULL && q!=NULL) 4. { 5. if(p==q) 6. { 7. //Loop is there 8. exit(0); 9. } 10. p=p->next; 11. q=(q->next)?(q->next->next):q->next; 12. } 13. 14. // there is no loop

What is the difference between declaration and definition?

In case of C Functions. Declaration: When we write the prototype of the function before using it, this is called function declaration. Definition: When we define code for the function with the function name itself, it is called function definition.
DECLARATION : declaration only creates the variable name and nothing exists against it. i.e no memory space is consumed. eg. extern a; DEFINITION : definition declares as well as assigns some value to the variable. eg. int a; even though we are not mentioning any value default value is provided by the C compiler. eg. int a=10;

Data structure means how the data is organized in memory.There are diferent kind of data structures. Some are used to store the data of same type and some are used to store different types of data. Linear datastructures are accessed non-sequentially... Example:- arr[8],arr[4]Non-Linear data stuctures are accessed sequentially..

You might also like