33. What are dangling pointers? How are dangling pointers different from memory leaks?
The dangling pointer points to a memory that has already been freed. The storage is
no longer allocated. Trying to access it might cause a Segmentation fault. A common
way to end up with a dangling pointer:
#include<stdio.h>
#include<string.h>
char *func()
{
char str[10];
strcpy(str,"Hello!");
return(str);
}
You are returning an address that was a local variable, which would have gone out of
scope by the time control was returned to the calling function. (Undefined behavior)
*c = malloc(5izeof(int));
free(c);
*c = 3; //writing to freed location!
In the figure shown above writing to a memory that has been freed is an example of
the dangling pointer, which makes the program crash.
A memory leak is something where the memory allocated is not freed which causes
the program to use an undefined amount of memory from the ram making it
unavailable for every other running program(or daemon) which causes the programs
to crash. There are various tools like O profile testing which is useful to detect
memory leaks on your programs.
void function(){
char *leak = malloc (10); //leak assigned but not freed
}
34. What is the difference between ‘g’ and “g” in C?
In C double-quotes variables are identified as a string whereas single-quoted
variables are identified as the character. Another major difference being the string
(double-quoted) variables end with a null terminator that makes it a 2 character
array.
35. What is a near pointer and a far pointer in C?
Near Pointer: In general, the near pointer can be considered because it is used
to hold the address, which has a maximum size of just 16 bits. We can't store an
address with a size larger than 16 bits using the near pointer. All other smaller
addresses that are within the 16-bit limit, on the other hand, can be stored.
Because we can only access 64kb of data at a time, you might assume the 16 bits
are insufficient. As a result, it is regarded as one of the near-pointer's biggest
drawbacks, which is why it is no longer commonly used.
Far Pointer: A far pointer is considered a pointer of size 32 bits. It can, however,
use the current segment to access information stored outside the computer's
memory. Although, in order to use this type of pointer, we usually need to
allocate the sector register to store the data address in the current segment.
36. Which structure is used to link the program and the operating system?
The file structure is used to link the operating system and a program. The header file
"stdio.h" (standard input/output header file) defines the file. It contains information
about the file being used like its current size and its memory location. It contains a
character pointer that points to the character which is currently being opened. When
you open a file, it establishes a link between the program and the operating system
about which file is to be accessed.
37. Suppose a global variable and local variable have the same name. Is it possible to access a global variable from a block where local variables are defined?
No. This isn’t possible in C. It’s always the most local variable that gets preference.
38. Which is better #define or enum?
If we let it, the compiler can build enum values automatically. However, each of
the defined values must be given separately.
Because macros are preprocessors, unlike enums, which are compile-time
entities, the source code is unaware of these macros. So, if we use a debugger to
debug the code, the enum is superior.
Some compilers will give a warning if we use enum values in a switch and the
default case is missing.
Enum always generates int-type identifiers. The macro, on the other hand,
allowed us to pick between various integral types.
Unlike enum, the macro does not have a defined scope constraint.
