C / C ++ pointer question

tl; dr - Could you talk about 4 comments in the first code snippet below? In particular, what deref means

I have long been a Java developer who wants to learn C ++. I came across this site aimed at developers in my situation.

   int x, *p, *q;
   p = new int;
   cin >> x;
   if (x > 0) q = &x;
   *q = 3;                 // 1. deref of possibly uninitialized ptr q
   q = p;
   p = new int;            // 2. potential storage leak (if x != 0 this
                           //     memory will not be returned to free storage)
   *p = 5;
   delete q;
   *q = 1;                 // 3. deref of deleted ptr q
   q = p;
   if (x == 0) delete q;
   (*p)++;                 // 4. deref of possibly dangling ptr p (if x is zero)

Although I thought I understood how pointers work, I find the comments hard to understand.

My welcome;

  • We either assign x (& * q, of course) to 3 OR, if q! = & X, then q has a fair value, since it was uninitialized, and we just assigned a random part of the memory to 3. I'm not sure how you can dereference that which is not initialized?
  • This is normal
  • What happened to dereferencing a remote pointer? After 'delete q' is * q pointless?
  • ? , , ?

, int-, ? ?

" "? , , , ;

int *x;
x = new int;
*x = 5;
x = new int; // Dereferencing the first bit of memory allocated.

. , ,

+5
5

: - , .

- - * ( ) . , .

  • ( NULL), , . , , . undefined - , - , , trap/hardware.

  • . p , . . , - , .

  • , UB.

  • True. UB x == 0. , , . , .

+6

:

   int x, *p, *q;
   p = new int;
   cin >> x;
   if (x > 0) q = &x;
   *q = 3;                 // <--- 1. deref of possibly uninitialized ptr q
                           // 
                           // q has never been set or is set to point at a non
                           // allocated memory location (if x > 0)
                           //
                           // If x is zero q is uninitialized (has random value).
                           // Therefore de-referencing it to set what it points at 
                           // to 3 has undefined behavior.

   q = p;
   p = new int;            // <--- 2. potential storage leak (if x != 0 this
                           //     memory will not be returned to free storage)
                           // 
                           // This comment is not true. The value pointed at by p
                           // was transferred to q before p was re-assigned. Thus 
                           // there is no potential memory leak. Both p and q are 
                           // now valid pointers.
   *p = 5;
   delete q;
   *q = 1;                 // <--- 3. deref of deleted ptr q
                           // 
                           // In the line above you returned the memory pointed at 
                           // by q back to memory management routines (via delete). 
                           // Technically q still points at the memory but you no 
                           // longer own that memory and thus writing to it has
                           // undefined behavior (because the memory management 
                           // system could have unloaded that chunk from virtual 
                           // memory or re-used it some other way).
   q = p;
   if (x == 0) delete q;
   (*p)++;                 // <--- 4. deref of possibly dangling ptr p (if x is zero)
                           // 
                           // q is reassinged to have the same pointer value as p.
                           // Then If x is zero we delete the pointer q (thus 
                           // returning it to the memory management pool). Since p 
                           // and q are the same pointer they both become invalid at
                           // point. Thus de-referencing p is undefined behavior if
                           // x is zero (because the memory was returned (via delete)

1 x (& * q, ) 3 OR, q!= & x, q , , 3. , -, ?

. :

3 ? , " q" * q ?

- ( ( , , ). , , undefined.

4 ? , , ?

, 3. ( , , )

" "? , , , ;

: .
, .

  • , , .
  • (, 3 ), , .
  • , (, 3 ), , .
+3

:

  • q . Dereference doc.. - if, x, (), x <= 0.
  • q , p. p . , p.
  • , q. . , " ".
  • x == 0, , q, p. , "" . delete p, x != 0 - .
+2

, , " ". ++ - "" - .

, , :

: int x, *p, *q;, , . ++ , , . , .

, 1. , q &x . q , , ++ 3 - . , ++ .

3. ++ - , , , . , q *q , , .

4. , 3., (x==0) , , , , , .

int .

, .

:

int *x;         <-- Declares a pointer and allocates it on the stack
x = new int;    <-- Allocate a new int on the heap and remember its address in x
*x = 5;         <-- Overwrite the new int on the heap.
x = new int;    <-- Another allocation and remember its address in x
                    Now we have forgotten where the first allocation was

, . , . null , .

+2

:

  • x . , x ( ). , if , , q. , *q = 3 .

  • ! . , p. . , p. delete q, p, x ( ).

  • , . . - , , , . , "undefined ".

  • . ( ) .

+1

All Articles