Serious problem about pointer!

May 25, 2015 at 5:56pm

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
29
30
31
32
33
34
35
  #include <iostream>
#include <cstdlib>
using namespace std;

struct node {
    int info;
    node* left;
    node* right;
};

void makeNode(node* r, int x)
{
    r = new node;

    r->info = x;
    r->left = NULL;
    r->right = NULL;

    cout << r->info << endl; // this prints 22
}

int main()
{
    node* A;

    A = new node;

    A->info = 11;

    makeNode(A, 22);

    cout << A->info; // it still prints 11 instead of 22

    return 0;
}


Hello, everyone! I need your help.
Please notice the above code and comments inside it. How to allocate a new piece of memory inside the makeNode(node* , int) function to the pointer passed as argument(in this case- A)? In the above case, A still points to the same memory previously allocated for it inside of main() function.
How can I accomplish this? How to make A->info print 22 after a call to makeNode(A, 22)? Notice the code and please help me.
Note: the return-type of makeNode(node*, int) function must be void
Last edited on May 25, 2015 at 5:58pm
May 25, 2015 at 6:11pm
Is it necessary for you to use pointers? You could do something like this (this is code I'm writing on the fly, sorry if it has any small errors in it):
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
#include <iostream>
#include <cstlib>
using namespace std;

struct node {
int info;
node* left, right;
};

void makeNode(node & r, int x)
{
r.info=x;
r.left=NULL;
r.right=NULL;

cout<<r.info<<endl;
}

int main()
{
node A;
A.info = 11;
makeNode(A,22);
cout<<A.info;
return 0;
}

Does that work? It uses passing by reference instead of pointer.
May 25, 2015 at 6:17pm
You have memory leak,try to fix that. Every time you call new you should call delete to clear the space you've taken.
May 25, 2015 at 6:19pm
That is one of the reasons I suggested avoiding pointers and using references. I couldn't be bothered to do another rant about RAII as it seems like a loosing battle.
May 25, 2015 at 6:41pm
@shadowmouse I think it's his homework, so you if I'm right you cannot just say "don't use it". And I'm sure if you had similar problem you'd like to know why it is wrong, and not what to use instead.
May 25, 2015 at 6:42pm
All Memory problems asside, you have to pass a reference to the pointer if you want to change it's content:
void makeNode(node*& r, int x)
May 25, 2015 at 6:51pm
No, this is just alternative.
I have met a problem where I must use pointer in the following way-
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
29
30
31
32
33
34
#include <iostream>
#include <cstdlib>
using namespace std;

struct node {
    int info;
    node* left;
    node* right;
};

void makeNode(node* r, int x)
{
    r = new node;

    r->info = x;
    r->left = NULL;
    r->right = NULL;

    cout << r->info << endl; // this prints 22
}

int main()
{
    node* A;

    A = NULL;

    makeNode(A, 22);

    cout << A->info; // crashes now!

    return 0;
}


I must not allocate memory for A inside main().
This program will crash! How may I fix this exactly using pointers in the above way? Let's talk about memory leak later. Please help.
Last edited on May 25, 2015 at 6:52pm
May 25, 2015 at 6:53pm
I allready should've answered your question by now...
May 25, 2015 at 7:06pm
First Gamer2015 is right, second you don't have a memory leak anymore, as you don't call new in your second example and third you need to call delete A before program termination.
May 25, 2015 at 7:18pm
@ Gamer2015!

Sorry, I think we were both posting at the same time, so I did not notice you.

Anyway, thanks! It worked! I don't know clearly why it worked, can you give me any reference?
Much obliged!!!
May 25, 2015 at 7:28pm
@TheHardew
Isn't it unnecessary to call delete A just before program termination? As per as I know all memories occupied by a program are automatically freed after program termination.
May 25, 2015 at 7:29pm
Normally parameters are just copied like here:
1
2
3
4
5
6
7
void makeNode(node* r, int x){
    r = new node;
    r->info = x;
    r->left = NULL;
    r->right = NULL;
    cout << r->info << endl; // this prints 22
}


You assign a value to copy of node*, A. When you use reference no new object is created and you access pointer itself. It would also work if you passed a pointer to pointer to node aka node** and tried to access pointer to node through its pointer, like here:

1
2
3
4
5
6
7
void makeNode(node** r, int x){
    *r = new node;
    (*r)->info = x;
    (*r)->left = NULL;
    (*r)->right = NULL;
    cout << (*r)->info << endl; // this prints 22
}


But reference is faster.

EDIT:
Isn't it unnecessary to call delete A[...]?
On most systems yes. But still it's good habit, e.g. if you write a video card driver.
Last edited on May 25, 2015 at 7:33pm
May 25, 2015 at 8:04pm
But reference is faster.

What do you mean by that?

Anyway, thanks! It worked! I don't know clearly why it worked, can you give me any reference?
Much obliged!!!

It's a little bit hard to explain without pictures and all but I'll try
it's the value of the pointer that's being copied, the other pointer in the main function is not effected

let's say you have this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
void test(int* b) {
    b= new int(10);
    std::cout << *b<< std::endl;
}

int main()
{
    int* a = new int(5);
    test(a);
    std::cout << *a << std::endl;

    return 0;
}


so you have a pointer in main which you pass to the function test.
The thing that's being passed is the value the pointer holds.
Therefore it's the same like this
1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
    int* a = new int(5);

    int* b = a; // both point to the same location
    b = new int(10); // a is not effected by that!!! a still points to the other integer
    std::cout << *b << std::endl; // 10

    std::cout << *a << std::endl; // 5

    return 0;
}


with the function, the parameter b is just an other pointer pointing to the same location as the pointer in your main function, but when doing new, the pointer in main isn't changed, only the local pointer is modified

if you take the reference of the pointer then you modify the pointer itself, you don't make a copy of the pointer and then modify the new pointer.
1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
    int* a = new int(5);

    int*& b = a; // b is now a reference to the int* a
    b = new int(10); // a is effected by that because b modifies the object it's referencing
    std::cout << *b << std::endl; // 10

    std::cout << *a << std::endl; // 10

    return 0;
}


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

void func(int*& b) {
    b = new int(10);
    std::cout << *b << std::endl;
}
int main()
{
    int* a = new int(5);
    func(a);

    std::cout << *a << std::endl; // 10

    return 0;
}
Last edited on May 30, 2015 at 7:51am
May 26, 2015 at 5:31am
Thanks a lot! I appreciate your help.
Last edited on May 26, 2015 at 5:36am
Topic archived. No new replies allowed.