in Programming in C edited by
20,850 views
78 votes
78 votes

Consider this C code to swap two integers and these five statements: the code

  void swap (int *px, int *py) 
     { 
        *px = *px - *py;
        *py = *px + *py;
        *px = *py - *px;
     }

S1: will generate a compilation error 

S2: may generate a segmentation fault at runtime depending on the arguments passed 

S3: correctly implements the swap procedure for all input pointers referring to integers stored in memory locations accessible to the process

S4: implements the swap procedure correctly for some but not all valid input pointers 

S5: may add or subtract integers and pointers

  1. S1
  2. S2 and S3
  3. S2 and S4
  4. S2 and S5
in Programming in C edited by
20.9k views

4 Comments

In S5:

May add or subtract integer and pointer : false statements

Reason: because pointer add or subtract for some references or swap purpose.

Only alone add pointer and subtraction of pointer is not valid.

Plz check I am right or wrong??
0
0
reshown by
we are not adding 2 pointers, rather we are adding the contents which these pointers points to.
1
1
Why S5 is not correct? here question is saying “may” add int and pointer ; “may” substract int and pointer . and because of “may”(possibility) we can say that it’s correct.
0
0

4 Answers

90 votes
90 votes
Best answer

S1 is false.

S2 is true, depending on the argument passed it may generate segmentation fault.

S3 is false because implementation is having some problem. Let $x=3$ and I want to implement $SWAP[x,x]$. Now ans would be $0$ but that must be $x$. Problem is because we are not checking whether both pointer are pointing the same address or different So, S4 is true.

S5 is obviously false so, option (C) is right.

edited by

4 Comments

Yeah. If we consider arithmetic overflow S3 is false, but notice "for all valid input pointers" in S4, so I think the context in question is pointers
1
1

 dangling pointers donot cause segmentation fault.

but wild pointers and dereferncing a null pointer does cause segmentation fault.

https://en.wikipedia.org/wiki/Segmentation_fault#Causes

#include <stdio.h>
#include <stdlib.h>

int main()
{
        int *p,*q,*r=NULL;
       *p=5;//wild pointer
       q=(int*)malloc(sizeof(int));
        *q=6;
        free(q);
        printf("%d",*q);// q is a dangling pointer(on running it gives an wrong output)
        printf("%d",*r);//dereferencing of null pointer.
        return 0;
}

0
0

Just want to add one more reason for why S3 is false apart from when pointing to the same number.

Reason- Overflow can also occur when swapping is done using addition and subtraction.

Ex : Assuming 2’s complement system for representation of numbers.

px points to maximum interger possible in that register.

py points to minimum interger possible in that register.

Then, *px-*py will give overflow.

So, swapping using addition and substraction is not always prefered.

9
9
21 votes
21 votes

Option C

S1:FALSE-It will generate runtime error(If occur),not compile time

S2:TRUE- May generate segmentation fault if value at pointers px or py is constant or px (or) py points to a memory location that is invalid (or) px or py may contain NULL. 
S4:TRUE- May not work for all inputs as arithmetic overflow can occur.

1 comment

Arithmetic Overflow will not occur as the address of the variables is passed to the function and the variable which stores that integer will wrap around the integer to other side and the int get stored thereafter. So, it will work for all valid input pointers.
0
0
8 votes
8 votes

S1: False 

S2:True

if we pass address NULL to px and NULL to py then there can be segmentation fault at runtime 

S3: False 

reason1:If we give address of single integer to both px and py

e.g

x=10;

swap(&x,&x) i.e.

px=&x

py=&x

now 

$*px = *px - *py;$

it will make x=0

$*py = *px + *py;$

x will still remain 0

$*px = *py - *px;$
x will still remain 0

so no swapping occurred

reason2:if we have very large two integer e.g

x=-32,768(max negative no. that int datatype can hold)

y=32,767(max positive no. that int datatype can hold)

now on calling swap(&x,&y)

*px = *px - *py
this line does $-32,768-32,768=65535$

int cannot hold 65535 so overflow occurred and it won’t swap the  numbers

S4:True

S5:False

Answer(c)

by
1 vote
1 vote

Option B is correct.

S1: False

S2: True. Dereferncing uninitialized pointer may generate segmentation fault, depend on argument passed.

S3: True. correctly implements swapping for each input. It can swap same integer value from different 'memory locations' (given in stmt)

S4: False.

S5: False.

1 comment

It unable to swap integers when one integer is +ve and other is -ve

 if both the value is same means swap(x,x) then it not work.
0
0
Answer:

Related questions