Call by reference problem

closed account (9267ko23)
Hi,

I have a problem with a function which allocates memory for the caller. I do the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;

void test(int *ip) {
    ip = new int;
    *ip = 5;
}

int main(int argc, char *argv[])
{
    int *ip = nullptr;
    test(ip);
//    ip = new int;
//    *ip = 5;
    cout << *ip << endl;
    
    return 0;
}


This call of test does not work. If I comment the function call and use the commented lines all works fine.

Can anybody tell me why? And can anybody tell me how to do this correct in c++?

I think there is something I didn't understand yet.
cout << *ip << endl;
You are dereferencing a null pointer.

Your code leaks memory.
You're not calling by reference, you're passing a pointer. There's a difference.

Change your code to this to see what I mean:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;

void test(int *ip) {
    ip = new int;
    *ip = 5;
    cout << "Points to " << ip << endl;
}

int main(int argc, char *argv[])
{
    int *ip = NULL;
    cout << "Points to " << ip << endl;
    test(ip);
    cout << "Points to " << ip << endl;
   //  cout << *ip << endl;
    
    return 0;
}


I'd imagine you probably want something like this...
1
2
3
4
5
6
7
8
int* test()
{
   int *my_ptr = new int(5);
   return my_ptr;
}

// In main...
int *ip = test();


Or something of that nature.

EDIT: Don't forget to free up that memory as well.
Last edited on
closed account (9267ko23)
Yes, I saw this, but I don't know why. Isn't the call a call by reference?
closed account (9267ko23)
Thank you for your help. This solves my problem. Now I solved the problem.
Nope, it's not a call by reference.

A pointer holds the address of whatever it is pointing to. For example, say the address of an int is 1234, the pointer would hold the value 1234.

When you pass a pointer to a function, it's the same as passing any other variable. That is, it'll create a copy of that point and work on that within the function. You're merely passing a value that holds an address, not passing the address itself.

Like any other variable, you can pass the address of a pointer (see following examples) . I think, however, you'd just be better off returning a pointer as above.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Method 1
void Assign(int*& my_ptr)
{
   my_ptr = new int(5);
}

// Call
int *int_ptr = NULL;
Assign(int_ptr);

// Method 2
void Assign(int** my_ptr)
{
   *my_ptr = new int(5);
}

// Call
int *int_ptr = NULL;
Assign(&int_ptr);

Last edited on
closed account (9267ko23)
OK thank you very much. It's very good to know that also a call with a pointer will put a copy of the pointer to the function.
Hello there, what you are not actually passing by reference . Instead try this :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using namespace std;

void test(int *ip)
{
     *ip = 5;
}

int main()
{
     int *ip;
     test(ip);
     cout<<" IP points to address location : "<<ip;
     cout<<"\n Value that IP points to : "<<*ip;
     return 0;
} // end of main()


Hope this helps :)
@techieboy - That's not going to work.
Topic archived. No new replies allowed.