1

I have something like this:

int* a = NULL;
int* b = *a;

b = new int(5);
std::cout << *a;
std::cout << *b;

I would like to instantiate a from b so a has the value 5. Is this possible?

EDIT:

actual code is something like this -

int* a = null; //Global variable
int* b = null; //Global variable

int* returnInt(int position)
{
    switch(position)
    {
      case 0:
         return a;
      case 1:
         return b;
     }
}

some other function -

int* c = returnInt(0); // Get Global a

if (c == null)
    c = new int(5);

i want to instantiate the global variables this way if possible.

kurupt_89
  • 1,542
  • 8
  • 37
  • 65
  • 3
    you can't dereference *a, its NULL – Mitch Wheat Oct 01 '12 at 11:41
  • @larsmans provided more information – kurupt_89 Oct 01 '12 at 11:50
  • @MitchWheat: Of course you can dereference a NULL pointer. You just shouldn't try to read or write the memory at adress zero. This is why accessing class methods/fields (i.e. those denoted by `static`) works via a null pointer. – Frerich Raabe Oct 01 '12 at 11:52
  • "instantiate" doesn't mean "assign a value to". You cannot "instantiate a variable", global or otherwise. Also, `b` does not "have the value 5" after the assignment from `new int(5)`. `b` has a pointer value. The referand of `b` has value 5. It may be that you want to write `a = b`, or it may be that you're thoroughly confused and you should do something else entirely. I'm too confused to tell. It is almost always wrong to write `new int`, though. – Steve Jessop Oct 01 '12 at 12:13
  • @FrerichRaabe - sure. Undefined behavior means that code can do pretty much anything, including doing exactly what you expect it to do. Of course, it will fail when you show your program to your most important customer. – Pete Becker Oct 01 '12 at 13:18
  • @PeteBecker: Mind sharing the section of the C++ standard in which it says that dereferencing a null pointer is undefined behaviour? I'd like to look it up in my copy (I only have the C++03 standard around tho) - always thought only the reading/writing part is undefined. – Frerich Raabe Oct 01 '12 at 13:33
  • 2
    @FrerichRaabe - 5.3.1 [expr.unary.op]/1 (C++11, but the requirements haven't changed since C++03) says "The unary * operator performs indirection: the expression to which it is applied shall be a pointer to an object type, or a pointer to a function type and the result is an lvalue referring to the object or function to which the expression points." If there's no object (i.e. a null pointer) this simply doesn't apply, so the behavior is undefined. Some folks want this changed to allow dereferencing, which could simplify some things, but that's in the future. – Pete Becker Oct 01 '12 at 13:50
  • @PeteBecker: Hm, I tend to agree. However - maybe I'm being a bit pedantic here - the part you quote says "and the result is an lvalue referring to the object" but not "and the result is the object". So many one could argue that the thing which `*((int*)0)` evaluates to is a perfectly fine `lvalue`, but it doesn't refer to an object. In any case, you convinced me - from now on I shall consider dereferencing a null pointer to be undefined. :-) – Frerich Raabe Oct 01 '12 at 15:12
  • 1
    @FrerichRaabe - there is not complete agreement on this interpretation, but I certainly wouldn't count on it being well defined. – Pete Becker Oct 01 '12 at 15:27
  • @PeteBecker: Reviving this discussion - I just noticed that the [`FIELD_SIZEOF`](https://github.com/torvalds/linux/blob/master/include/linux/kernel.h#L61) macro in the `include/linux/kernel.h` file of the Linux kernel sources actually dereferences a null pointer as well. Does that mean that it relies on undefined behaviour, or does the C standard define it to be valid? – Frerich Raabe Oct 10 '12 at 21:50

2 Answers2

3

You need a reference:

int* b = NULL;
int*& a = b;

Any changes to either a or b will impact the other one.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
3
int* a = NULL;
int* b = *a; //here you dereference a NULL pointer, undefined behavior.

You need

int* b = new int(5);
int*& a = b; //a is a reference to pointer to int, it is a synonym of b
std::cout << *a;
std::cout << *b;

Or, a could be a reference to int and be a synonym for *b

int* b = new int(5);
int& a = *b; //a is a reference to int, it is a synonym of `*b`
std::cout << a;  //prints 5
std::cout << *b; //prints 5
a = 4;
std::cout << a;  //prints 4
std::cout << *b; //prints 4

Please consult a good C++ book for details.

Community
  • 1
  • 1
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434