0

One function is pass by pointer which stores the reference, the other is just pass by a reference... are theses methods equvilent or does one method require more memory than the other?

#include <iostream>
using namespace std;

void cubeByRef(int *num);
void cubeByRef2(int &num);

int main(){
    int a = 2;
    cubeByRef(&a);
    cout << "cubeByRef : " << a << endl;

    a = 2;
    cubeByRef2(a);
    cout << "cubeByRef2: " << a << endl;

    return 0;
}

void cubeByRef(int *num){
    *num = *num * *num * *num;
}

void cubeByRef2(int &num){
    num = num * num * num;
}
2
Contributors
3
Replies
4
Views
9 Years
Discussion Span
Last Post by Ancient Dragon
0

>> are theses methods equvilent
yes (almost). There are a couple things that can be done with pointer num that can not be done with the reference.

>> or does one method require more memory than the other?
no

0

Other than pointer arithmetics or something I don't know anything else... but the memory thing was what I wanted to know...
cheers

0

The internal code produced by your compiler most likely is the same for both functions, so there would be no difference in memory requirements. The difference between the two functions is the c++ language requirements, compilers may implement them however it wants to.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.