Memory leak occurs when programmers create a memory in heap and forget to delete it. Memory leaks are particularly serious issues for programs like daemons and servers which by definition never terminate. #include <stdlib.h> void f() { int* ptr = (int*)malloc(sizeof(int)); return; /* Return without freeing ptr*/ }
-
-
Dangling Pointer is a pointer that doesn’t point to a valid memory location. Dangling pointers arise when an object is deleted or deallocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the deallocated memory. Following are examples. int* ptr = (int*)malloc(sizeof(int)); ..........................free(ptr); // ptr is a dangling pointer now and operations like following are invalid *ptr = 10; // or printf("%d", *ptr); int* ptr = NULL { int x = 10; ptr = &x; } // x goes out of scope and memory allocated to x is free now. //…
-
The mistakes when creating a program called syntax errors. Misspelled commands or incorrect case commands, an incorrect number of parameters when called a method /function, data type mismatches can identify as common examples for syntax errors.
-
By default every local variable of the function is automatic (auto). In the below function both the variables ‘i’ and ‘j’ are automatic variables. void f() { int i; auto int j; } NOTE − A global variable can’t be an automatic variable.
-
Int – Represent the number (integer) Float – Number with a fraction part. Double – Double-precision floating-point value Char – Single character Void – Special purpose type without any value.
-
Both allocates memory from heap area/dynamic memory. By default calloc fills the allocated memory with 0’s
-
It’s a pointer variable which can hold the address of another pointer variable. It de-refers twice to point to the data held by the designated pointer variable. Eg: int x = 5, *p=&x, **q=&p; Therefore ‘x’ can be accessed by **q.
-
Portability – Platform independent language. Modularity – Possibility to break down large programs into small modules. Flexibility – The possibility to a programmer to control the language. Speed – C comes with support for system programming and hence it is compiling and executes with high speed when compared with other high-level languages. Extensibility – Possibility to add new features by the programmer.
-
NULL is used to indicate that the pointer doesn’t point to a valid location. Ideally, we should initialize pointers as NULL if we don’t know their value at the time of declaration. Also, we should make a pointer NULL when memory pointed by it is deallocated in the middle of a program.
-
1. To get address of a variable 2. For achieving pass by reference in C: Pointers allow different functions to share and modify their local variables. 3. To pass large structures so that complete copy of the structure can be avoided. 4. To implement “linked” data structures like linked lists and binary trees.