0% found this document useful (0 votes)
40 views6 pages

Jaga

This document contains information about dangling pointers, bit rotation, and bit masking in C programming. It defines a dangling pointer as a pointer that references memory that has been deallocated. It provides examples of how dangling pointers can cause issues. It also explains bit rotation as rotating bits from left to right or vice versa, and provides a code example. Finally, it defines bit masking and explains how bitwise operators can be used for bit masking in C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views6 pages

Jaga

This document contains information about dangling pointers, bit rotation, and bit masking in C programming. It defines a dangling pointer as a pointer that references memory that has been deallocated. It provides examples of how dangling pointers can cause issues. It also explains bit rotation as rotating bits from left to right or vice versa, and provides a code example. Finally, it defines bit masking and explains how bitwise operators can be used for bit masking in C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

CSE ASSIGNMENT 4&5

1) DANGLING POINTER:::

A dangling pointer in C is a pointer that


references memory that has been deallocated, such
as a dynamically-allocated block of memory that has
been freed. Dereferencing a dangling pointer can
lead to undefined behavior or a segmentation fault.
In simple words dangling pointer in c refers to or
points to the memory which has been already
deallocated such as dynamic memory which is not in
use or deallocated.

➤ Steps for dangling memory-----

1) De-allocation of Memory:
The allocation and deallocation of memory blocks are carried out using library
functions, such as malloc(), calloc(), and free().
2) Function Call:
When the function is finished, all internal variables go through garbage
collection and are no longer in memory, but the main() function’s pointer is still
pointing to that specific address, which is no longer available in memory,
creating a dangling condition.
3) Variable Goes out of the Scope:
A Dangling Pointer in c outside the inner block of code if the address of this
local variable is assigned to a pointer declared outside the scope.

EX: #include <stdio.h>


int *fun(){
int y=10;
return &y;
}
int main()
{
int *p=fun();
printf("%d", *p);
return 0;
}
OUTPUT: SEGMENT FAULT
o First, we create the main() function in which we have
declared 'p' pointer that contains the return value of the fun().
o When the fun() is called, then the control moves to the context of
the int *fun(), the fun() returns the address of the 'y' variable.
o When control comes back to the context of the main() function, it
means the variable 'y' is no longer available. Therefore, we can say
that the 'p' pointer is a dangling pointer as it points to the de-
allocated memory.

2)MCQ’S

a) What is the correct syntax of if statement in C program?

A. if(condition) {
}
B. if(condition) :
C. If { [condition] }
D. None of these
b) #include <stdio.h>
int main()
{
int i = 3;
printf("%d", (++i)++);
return 0;
}

A.3

B.4

C.5

D.Compile –time Error

c) When all parts of the for loop are eliminated, what will happen?

A. For loop will not work


B. Infinite for loop
C. Error None of
these D.

d) Which function is used to concatenate two strings in C?


A. concat()
B. cat()
C. stringcat()
D. strcat()

e) Which of the following statements correct about k used in the below statement?
char ****k;

a) k is a pointer to a pointer to a pointer to a char


b) k is a pointer to a pointer to a pointer to a pointer to a char
c) k is a pointer to a char pointer
d) k is a pointer to a pointer to a char

3)BITS ROTATION:::
A rotation (or circular shift) is an operation similar to shift except

that the bits that fall off at one end are put back to the other end. In left

rotation, the bits that fall off at left end are put back at right end. In right

rotation, the bits that fall off at right end are put back at left end.

• Rotating the bit from left to right or right to left.


• In left rotation, the bits are shifted from left to right.
• In right rotation, the bits are shifted from right to left.
EX--------#include<stdio.h>
#include<stdlib.h>
int main(){
int number, rotate, Msb, size;
printf("Enter any number:");
scanf("%d",&number);
printf("Enter number of rotations:
");
scanf("%d",&rotate);
size = sizeof(int) *
8; rotate %= size;
while(rotate--){
Msb = (number >> size) & 1;
number = (number << 1) | Msb;
}
printf("After Left rotation the value is = %d
",number);
return 0;
}
Output: Enter any number:12
Enter number of rotations:2
After Left rotation the value is = 4

➤BITS MASKING:::
Bit masking is simply the process of storing
data truly as bits, as opposed to storing it as
chars/ints/floats. It is incredibly useful for storing certain
types of data compactly and efficiently. The idea for bit
masking is based on boolean logic.

➤➤In C programming, we use Bitwise operators for bit


masking. They are-

1. & (bitwise AND) : The result of AND is 1 only if


both of the bits are 1.
2. | (bitwise OR) : The result of OR is 1 if either of
the bits are 1.
3. ^ (bitwise XOR) : The result of XOR is 1 if the bits
are different (one 0 and the other 1), and 0 if the bits
are
the same (both 0s or both 1s).
4. << (left shift) : Left shifts the bits of the first
operand by places specified in the second operand.
5. >> (right shift) : Right shifts the bits of the first
operand by places specified in the second operand.
6. ~ (bitwise NOT) : The result is the opposite of
the input value (1 -> 0, 0 -> 1).
EX ::: #include<stdio.h>
int main() {

int number;

printf("Enter any integer: ");

scanf("%d",&number);

if(number % 2 ==0)

printf("%d is even number.",number); else

printf("%d is odd number.",number);

return 0;

Result: Enter any integer: 5


5 is odd number

DONE BY—
G.DINESH SWAMY;
CSE-O;
AP22110010960;

You might also like