pass by pointer and pass a NULL

Hi all, in below test code, if I pass a NULL pointer to the add(int*) function, I still couldn't get the allocated space in my main function. And it thus gave me segmentation fault.

Could you advise why? A workaround for this issue would be pass by a reference of a point, eg,

void add(int* &i)

Is there any other solutions? Thanks very much.

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

using namespace std;

void add(int* i) {
  if(i==NULL) {
    i=new int(0);
    (*i)++;
    return;
  }

  (*i)++;
}

int main(int argc, char **argv)
{
  int* i=NULL;
  add(i);

  cout << *i << endl;

  return 0;
}


Your function header is different than what you list.

void add(int* &i)

This is a reference to a pointer-to-int.

void add(int* i)

This is just a pointer-to-int (which is passed by value).

Hence, your function cannot modify the i in main() (declared on line 17). If you fix your add() function to take the proper type of argument, it will work.

You should also remember to delete i; before you return 0;.


One last thing: The name "add" is not properly informative. Use "increment" or "inc" or "incr" or "bump" etc.

Hope this helps.
Hi Duoas, your reply is very helpful. Thanks. So what you mean is the function cannot modify i (the pointer itself) in main(). However if I pass a non-NULL pointer, and I can get the updated value pointed by i.

Maybe I mixed two concepts here, the pointer itself and the value pointed by the pointer? Thanks.

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
#include <iostream>

using namespace std;

void inc(int* i) {
  if(i==NULL) {
    i=new int(0);
    (*i)++;
    return;
  }

  (*i)++;
}

int main(int argc, char **argv)
{
  int* i=new int(10);

  inc(i);
  cout << *i << endl;

  delete i;

  return 0;
}
Last edited on
If this helps,

the pointer itself is just an address.

cout << i << endl; will just cout the address or 0 if it is set to NULL.

When you write *i you are referring to the value pointed to by i.


And just to be safe, I always check if a pointer is NULL before I dereference it by writing if (i)

This will return false if the pointer i is set to NULL. Dereferencing a NULL pointer will result in a run-time seg fault. They are not fun.
Last edited on
Yes.

On line 7, you are modifying the pointer i.
On line 8/12, you are modifying the value that i is pointing at.

They are different.
...which is what he intended, of course. The problem is that he is confused by combining the ideas between pointers and references and the redirections complicit in each...

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>

using namespace std;

void inc(int* &i) {
  if(i==NULL) {
    i=new int(0);
    (*i)++;
    return;
  }

  (*i)++;
}

int main(int argc, char **argv)
{
  int* i=NULL;

  inc(i);
  cout << *i << endl;

  inc(i);
  cout << *i << endl;

  return 0;
}

Hope this helps.
Topic archived. No new replies allowed.