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;
}

Recommended Answers

All 3 Replies

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

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.