Distributed Computing
Lab2
2025
Agenda
• Passing by value, pointer, reference.
• 2-D dynamic arrays.
• Static vs. dynamic arrays.
Passing by value, pointer, reference…
Passing by value, pointer, reference
• C++ offers different ways to pass arguments to function
• by value : void f(int x);
• by reference : void f(int& x);
• by pointer : void f(int* x);
• In passing by value, the function obtains only a local copy of the
variable, so that changes to the local variable have no impact on
the argument with which the function was invoked
• In passing by reference and passing by pointer, the function
manipulates the original variable rather than only a copy of it
Passing by value, pointer, reference Cont.
void swap( double & x, double & y) //Pass by reference
{
double tmp=x;
x=y; // access variable by its alias name
y=tmp;
}
void swap( double * ptr1, double * ptr2) //Pass by pointer
{
double tmp=*ptr1;
*ptr1=*ptr2; // de-referencing pointer
*ptr2=tmp;
}
void swap( double a, double b) //Pass by value
{
double tmp=a;
a=b; //
b=tmp;
}
Passing by value, pointer, reference Cont.
• A pointer is a variable that holds a memory address. A reference it is not a variable, it is
the memory address of the item itself.
• A reference must refer to an object. Pointer can be assigned to NULL, whereas
reference cannot. Since references can’t be NULL, they are safer to use.
• A reference is essentially a valid pointer. A pointer is not always valid.
• A pointer can be re-assigned. Reference cannot and must be assigned at initialization
only.
• A pointer needs to be dereferenced with * to access the memory location it points to,
whereas a reference can be used directly.
Use references when you can
and pointers when you have to
2D Dynamic Array…
2D dynamic array
2D dynamic array Cont.
delete [] arr2D;
arr2D = NULL;
Hands On ☺
2D dynamic array Cont.
int rows, cols;
cin >> rows >> cols;
// dynamic allocation
int** ary = new int*[rows];
for(int i = 0; i < rows; ++i)
ary[i] = new int[cols];
// fill
for(int i = 0; i < rows; ++i)
for(int j = 0; j < cols; ++j)
ary[i][j] = i;
2D dynamic array Cont.
// print
for(int i = 0; i < rows; ++i){
for(int j = 0; j < cols; ++j)
cout << ary[i][j] << '\t';
cout << endl; }
// free
for(int i = 0; i < rows; ++i)
delete [] ary[i];
delete [] ary;
ary = NULL;
Static vs. dynamic arrays…
1D and 2D
static Arrays
• Size: Both will require the same
amount of memory.
• Speed: You can assume that
there will be no speed
difference because the memory
for both arrays should be
contiguous (The whole 2D array
should appear as one chunk in
memory rather than a bunch of
chunks spread across memory.
So, Only the address of first
element is “memorized” and
the rest are calculated ).
1D and 2D
dynamic Arrays
• Size: The 2D array will require
a tiny bit more memory than
the 1D array because of the
pointers needed in the 2D
array to point to the set of
allocated 1D arrays.
• Speed: The 1D array may be
faster than the 2D array
because the memory for the
2D array would not be
contiguous, so cache misses
would become a problem.
Thank You ☺