simple (?) pointer problem. pointer value lost when returning from a function

Hello,
I'm using a function to make a pointer point to some dinamically allocated memory (allocated by the function). The problem is that when the function returns, the pointer doesn't keep the new address.
I don't understand what I'm doing wrong. Here I post a simple example that shows what I mean.

this is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <cstdlib>
#include <iostream>

using namespace std;
void f(int* p);


void f(int* p) {

    int* n = (int*) malloc(sizeof(int));
    p = n; 
    printf("in f(): n = %d, p = %d.\n\n", n, p);
}


int main(int argc, char *argv[]) {

    int* p = NULL;
    
    printf("before calling f(): p = %d.\n\n", p);

    f(p);

    printf("after calling f(): p = %d.\n\n", p);

    system("PAUSE");
    return EXIT_SUCCESS;
}


and this is the output:
before calling f(): p = 0.

in f(): n = 9637552, p = 9637552.

after calling f(): p = 0.


Any idea?
Thank you in advance.
You need to pass the pointers by reference, not value.

The way you are doing it the function receives a copy of the pointer rather than the pointer itself. So you modify the copy and that gets destroyed when the function returns.

By passing a reference you modify the pointer that you passed in rather than a local copy:

 
void f(int*& p);
Perfect, thanx a lot Galik.

This solution is not legal in C, is it?
Should I use
1
2
3
int* f() {...}
...
int* p = f()

in order to make it compatible with C code o there is a similar way to do it?
Changing a pointer address the way you wrote before is so convenient.

For example, working with trees this way:
1
2
3
4
5
6
7
void add(struct Node*& root, struct Node* node) {
    if(root==NULL)
        root = node;
    else {
        "...change some other pointers eventually modifying the root..."
    }
}

I don't have to update the root everytime I call that function.
But if I use something like:
1
2
3
4
5
6
7
8
struct Node* add(struct Node* root, struct Node* node) {
    if(root==NULL) {
        root = node;
    else {
        "...change some other pointers eventually modifying the root..."
    }
    return root;
}

I should always update the root, even though I don't modify it.
Last edited on
To remain compatible with C you would need to pass a pointer to your pointer:

1
2
3
4
5
6
7
void f(int** p) // note pointer to pointer to int
{

    int* n = (int*) malloc(sizeof(int));
    *p = n; // change what p points to (the pointer you want to modify) 
    printf("in f(): n = %d, p = %d.\n\n", n, *p);
}


Notice that the p needs to be dereferenced with * to treat it like a pointer compared with simply using p as you did before.

Also, malloc() is good for C but you should really use new for C++.
I don't really understand why you are including C++ headers but using C functionality.
Thanx Galic, that's a better solution than returning a pointer everytime.

ps: yes I forgot to change the headers. I planned to use C but I don't have a Unix OS anymore and Dev-C++ was reporting its beautiful [build error]. So I switched to C++.
I'm not doing professional things or similar, just studying some algorithm techniques (red-black trees today) so I didn't care a lot about headers. In reality the first Galik's reply was exactly was I was looking for, but I took my C book and could not find such a thing in there so I was wondering if there were a similar approach in C. In the end I solved the Dev-C++ boring problem too.

Sorry for the late reply and thank you again! Today I learned more than I expected!
Topic archived. No new replies allowed.