• edited by
51,046 views
85 85 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

10 Answers

Best answer
83 83 votes

Option is $A$.

In computer programming, aliasing refers to the situation where the same memory location can be accessed using different names. For instance, if a function takes two pointers $A$ and $B$ which have the same value, then the name $A$ aliases the name $B.$

• edited by
63 63 votes

B)multiple variables having the same value

int a=24;
int b=24;
int c=24;

 C)multiple variables having the same identifier

int a=23;
char a='A';

D)multiple uses of the same variable

int a=23;
 a=a*a;

A)multiple variables having the same memory location

int a=20;
int *p=&a;

This example also good http://www.cs.uregina.ca/Links/class-info/cplusplus/Standards/Disk10/aliasing_c.html

16 16 votes
A OPTION
5 5 votes
int $i=10;$
int $*p=\&i; $
As long as $p$ points to $i, $ we say that $*p$ is an alias for $i.$
• edited by
2 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 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) 

Answer:
Position:
Show:

Related questions

51 51 votes
8 answers 8 answers
17.6k
17.6k views
Kathleen asked Sep 14, 2014
17,561 views
A multiset is an unordered collection of elements where elements may repeat any number of times. The size of a multiset is the number of elements in it, counting repetiti...
81 81 votes
4 answers 4 answers
32.1k
32.1k views
Kathleen asked Sep 14, 2014
32,094 views
Consider the following C declaration:struct { short x[5]; union { float y; long z; } u; )t;Assume that the objects of the type short, float and long occupy $2$ bytes, $4$...
79 79 votes
4 answers 4 answers
19.6k
19.6k views
Kathleen asked Sep 14, 2014
19,553 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...
60 60 votes
5 answers 5 answers
24.6k
24.6k views
Kathleen asked Sep 14, 2014
24,551 views
The following C declarations:struct node { int i: float j; }; struct node *s[10];define s to be:An array, each element of which is a pointer to a structure of type nodeA ...