in Programming edited by
7,711 views
32 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
in Programming edited by
7.7k views

1 comment

Subscribe to GO Classes for GATE CSE 2022

8 Answers

39 votes
 
Best answer

Option is A.

In computer programmingaliasing 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

4 Comments

In sql aliasing means temporarily rename a table.same concept in c ?
2

@MRINMOY_HALDER

is sql a programming language as mentioned in the question?

0

 

there is no programing language mentioned in the question.

0

For instance, if a function takes two pointers A and B which have the same value, then the name A aliases the name B.

Can you give a code snippet for the above statement as it will make it more clear .

0
35 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

17 Comments

@srestha Is your example for option (C) allowed in code?
1
why will it not allow?
0

Try running this code- http://ide.geeksforgeeks.org/ccAyN2

1
yes, it is giving error because C doesnot allow multiple variable with same name

it is the example, what option C) is telling
1
As far as I know option (C) can't be implemented in real. Correct me if I am wrong.
0
Two pointers pointing to the same memory location is a good example of aliasing.

Can we relate aliasing with the union?
1

Example of multiple variables having the same identifier -

void fun1()
{
int t =10;  // local variable of function fun1.
printf("%d",t);
} 

void fun2()
{
int t=11;  // local variable of function fun2.
t= t+t;
printf("%d",t);
}

int main()
{
fun1();
fun2();
return 0;
}
5

@Soumya

Can we define a pointer as a variable?

In question they are asking just for a variable and not pointer

https://stackoverflow.com/questions/17643036/whats-the-difference-between-a-pointer-and-a-pointer-variable

2
@Srestha..Yes, we can define. Pointer is just a variable that can hold address of another variable.
4
@Soumya

why r u relating union here?

"A union is a variable that may hold (at different times) objects of different types and sizes,"

but union is not about same memory location

right?
0

do u mean. in union for a highest memory location , a memory will be created,

but in structure,we need to create memory for every variable

like told here

0

@Srestha.
Aliasing describes a situation in which a data location in memory can be accessed through different symbolic names in the program.
Now I meant to say that-
Consider this union.

union { 
        float y; 
        long z; 
    } u;

Here 8 Bytes of memory will be allocated for it. Say from 1000 to 1008.
Now we can access this memory location using &u.y and &u.z.

So can we say it aliasing?

3
do u mean union always operate on the concept of aliasing?

That is not true I think
0
@srestha . Any counter example ?
0
1
i think union always operate on the concept of aliasing just the issue is that obviously we cannot access the value assigned to the variable by some other variable with distinct data type.
0
@Soumya

actually variable and identifier refers to same in C programming

right?
0
15 votes
A OPTION
by

4 Comments

Call by reference is the best example for that.

3
we can relate to SQL query also...

SQL aliases are used to give a table, or a column in a table, a temporary name.
3
C Programming Language does not allow call by reference.
0

@akash.dinkar12

is sql a programming language as mentioned in the question? SQL (Structured Query Language) is a database management language for relational databases. SQL itself is not a programming language.

Though for argument's shake one may include it as programming language which maybe valid, but the spirit of the question is against it.

0
2 votes
int i=10;
int *p=&i;
As long as p points to i,  we say that *p is an alias for i.

2 Comments

You sure? Because if i has a memory location 100 then p will point to i but p can have a different memory location, say 200.
0

@Sambhrant Maurya

yes and for Option A to be true by this argument, variables here are

1. i

2. p

only. And clearly,

" multiple variables having the same memory location " (option A)

is not the case here. Though the, memory location

&i can be accessed by two ways :
&i and *p.

Logic of UNION gives OPTION A. Not this argument

 

0
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.

 

1 comment

As the answer is already long. Here I am commenting why OPTION C is wrong despite having many supporters here.

Yes, a variable identifier can be used multiple times in a program as we have the concept of scope. Multiple variables with same name may exist but they all would have different locations and are all essentially different.

#include<stdio.h>
/* global variable x */
int x ;
int x = 10;
int x;
/* 
all these 3 lines represent same variable in memory
line 2 : declaration
line 3: definition and initialization
line 4: forward declaration
*/
/* global variables are stored in global symbol table of c compiler */
void printing()
{
	printf("Address of GLOBAL x: %p\n", &x);
}

void printing_local_x()
{
	/* local variable x*/
	int x = 11;
	/* definition + initialization */
	printf("Address of LOCAL_2 x: %p\n", &x);
}

int main()
{
    /* local variable x*/
    int x;
    /* int x = 11; wrong */
    /* local variables are STACK VARIABLES and can only be defined/declared once */
    printing();
    printf("Address of LOCAL x: %p\n", &x);
    printing_local_x();
    return 0;
}

The output is:

Address of GLOBAL x: 0x5592945a0010
Address of LOCAL x: 0x7fff9cce20b4
Address of LOCAL_2 x: 0x7fff9cce2094

 

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) 

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
by
–5 votes
ans c)

1 comment

Option a is more appropriate.
2
Answer:

Related questions

Ask
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true