edited by
24,739 views
42 votes
42 votes

Aliasing in the context of programming languages refers to

  1. multiple variables having the same memory location
  2. multiple variables having the same value
  3. multiple variables having the same identifier
  4. multiple uses of the same variable
edited by

9 Answers

2 votes
2 votes

Option A is my answer.

Let us analyze.

"Aliasing" means more than one name for a memory location. If one address can be accessed by two or more "SYMBOLIC NAMES".

Most close option is OPTION A.

"multiple variables having the same memory location"

But, here we have to assume symbolic names to be the same as variable identifiers.

int *ptr;
int i;
/* &i : is a symbolic variable
    *p: is a symbolic variable */
    

But only

/* i and p : are variables */

So, even though we can access the memory location &i, through two symbolic names, this is not the correct argument for OPTION A.

Now, taking the concept of UNION data type solves the issue.

#include <stdio.h>

int main()
{
	union u
	{
		float f;
		unsigned u;
	} fu;
	printf("The address of the variable fu.f : %p\n", &fu.f );
	printf("The address of the variable fu.u : %p\n", &fu.u );
	printf("The address of the variable fu : %p\n", &fu);

	return 0;
}

Produces the output:

The address of the variable fu.f : 0x7fff764ceb84
The address of the variable fu.u : 0x7fff764ceb84
The address of the variable fu   : 0x7fff764ceb84

So, option A has been proved. Here multiple variables are having same memory location.

Though , I would like to point out that the language of OPTION A  has made some common aliasing examples ineligible for argument here:

1. Two pointers pointing to same memory location

2. Pointer to a variable, as both the pointer as well as the variable has access to same memory location

But the positive examples are:

1. UNION

2. ARRAY OVERFLOW

# include <stdio.h>

int main()
{
  int arr[2] = { 1, 2 };
  int i=10;

  /* Write beyond the end of arr. Undefined behaviour in standard C, will write to i in some implementations. */
  arr[2] = 20;

  printf("element 0: %d \t", arr[0]); // outputs 1
  printf("element 1: %d \t", arr[1]); // outputs 2
  printf("element 2: %d \t", arr[2]); // outputs 20, if aliasing occurred
  printf("i: %d \t\t", i); // might also output 20, not 10, because of aliasing, but the compiler might have i stored in a register and print 10
  /* arr size is still 2. */
  printf("arr size: %d \n", (sizeof(arr) / sizeof(int)));
}

Here also the LOCATION OF TWO VARIABLES:

1. arr[2], and

2. i

are same.

 

0 votes
0 votes

I know this question was asked in 2000 but if asked again its answers would be different, 

Correct answers would be A and C,

 A is called pointer aliasing(C) and C is called (Aliasing or Symbolic Aliasing, like in RUST) 

0 votes
0 votes
A because aliasing  in programming context refers to same memory mapped to different name i.e access can be done to same memory via different names
0 votes
0 votes

option A multiple variables having the same memory location.

int a  = 5;  // let the environment bound identifier ‘a’ to memory location 1000(Hex).

int* p;  // let the environment bound identifier ‘p’ to memory location 5000(Hex).

p = &a; // p will hold address of ‘a’. So, it will point to memory location 1000.

*p will refer to the value stored at location 1000.

printf(“%d”, a) and printf(“%d”, *p) both will print 5 as both having same memory location.

So, we can say ‘*p’ is alias of ‘a’.

option B multiple variables having the same value.

int a = 5;

int b = 5;

variables ‘a’ and ‘b’ are having different identifiers, so no problem having then in same scope or in different scope.

option C : multiple variables having the same identifier.

This can be done in two ways, 

First : Having same name variables in different scope.

example 

#include <stdio.h>
void f1(){
    int b = 20;
    {
        int b = 30;
        printf("%d\n", b);
    }
    printf("%d\n", b);
}
int main() {
    // Write C code here
    int a = 100;
    printf("%d\n", a);
    f1();
    return 0;
}

Second : What you take for that is the union and with that you actually have the same name of that variable, it does share the same memory, but it has a different extension.

example

  • #include <stdio.h>
  • int main(){ 
  • typedef union{ int i; float f;} flint;
  • flint a; 
  • a.i=1234; 
  • printf(“a.i=%d\n”, a.i); 
  • a.f=1.234; 
  • printf(“a.f=%f\n”, a.f); 
  • printf(“a.i=%d\n”, a.i); 
  • printf(“size=%ld\n”, sizeof a); 

option D multiple uses of the same variable

int a = 5;

int b = a;

variable ‘a’ used twice.

 

 

 

Answer:

Related questions

52 votes
52 votes
3 answers
2
Kathleen asked Sep 14, 2014
11,900 views
The most appropriate matching for the following pairs$$\begin{array}{|ll|ll|}\hline X: & \text{m = malloc(5); m = NULL;} & 1: & \text{using dangling pointers} \\\hline Y...
29 votes
29 votes
2 answers
4
Kathleen asked Sep 14, 2014
15,053 views
The value of $j$ at the end of the execution of the following C program:int incr (int i) { static int count = 0; count = count + i; return (count); } main () { int i, j; ...