Does a pointer pointed to nullptr need memory?

(text needed to post)
Your question is poorly framed.

A pointer may or may not require memory to exist - what it points to is irrelevant.
If you are worried whether a variable requires memory, then I think you have bigger problems.

We have the circular argument that you have 1 billion pointers that all point to nothing, what happens if they are all changed to point at some data?
closed account (E0p9LyTq)
Does a pointer pointed to nullptr need memory?


1
2
3
4
5
6
7
8
#include <iostream>

int main()
{
   int* ptr = nullptr;
   
   std::cout << &ptr << '\n';
}
The pointer itself will take up however much memory is taken up by a pointer of that type. The pointer will not be pointing to anything, so the only memory used at that point is just the memory to store the pointer.
If you have a pointer that never points to anything and you never use it, your compiler will probably just optimize it away.
> Does a pointer pointed to nullptr need memory?

If the value category of the pointer is prvalue (pure rvalue), typically it does not need any memory.

If the value category is lvalue, the observable behaviour of the program is "as-if" it is an object; that it occupies some area of memory.

If the value of the pointer is nullptr it dos not contain an address;
if not, the observable behaviour of the program is "as-if" it contains an address (points to some area in memory.)

As-if rule: http://en.cppreference.com/w/cpp/language/as_if

For example, foo() and bar() would typically generate identical 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
29
30
31
32
33
34
35
36
37
38
39
40
41
static int fn( int* p ) { return p ? *p : 0 ; }

static int fn( int** pp ) { return pp ? fn(*pp) : 0 ; }

int foo()
{
    int a = 7 ;
    int b = 8 ;

    int* pa = &a ;
    int* pb = &b ;

    int* temp = pa ;
    pa = pb ;
    pb = temp ;
    
    int** ppa = &pa ; // address of pointer pa: behave "as-if" pa is placed in some area of storage
    int** ppb = &pb ; // address of pointer pb: behave "as-if" pb is placed in some area of storage

    if( **ppa > **ppb ) ++*pb ;
    else ++**ppa ;

    if( ppa == ppb ) a = 123456 ;
  
    const int c[52] = { 1, 2, -3, -**ppa, -**ppb, *pa, *pb } ;
    int sum = 0 ;
    for( int i = 0 ; i < 7 ; ++i )
    {
        const int* ptr = c + i ;
        sum += *ptr ;
    }
  
     int** pptrs[] { ppa, ppb } ;
     int*** ppp1 = pptrs ;
     int*** ppp2 = &( pptrs[1] ) ;
    
    return **ppa - **ppb + sum - fn(ppa) + fn(pb) + 6 + ( ppp2 - ppp1 ); 
    // return 7 ;
}

int bar() { return 7 ; }

https://godbolt.org/g/Er84Io

For this program,
1
2
3
4
5
6
7
8
#include <iostream>

int main()
{
   int* ptr = nullptr;
   
   std::cout << &ptr << '\n';
}

the only requirement imposed by C++ is that it must print out the representation of some valid non-null address on the particular implementation.
closed account (48T7M4Gy)
http://stackoverflow.com/questions/13223399/deleting-a-pointer-in-c
Topic archived. No new replies allowed.