-2

I have a few silly questions (probably for most), it's less about 'how' do I do them and more about 'why' do they work this way? I know you are not supposed to ask multiple questions, but these are fairly small and related to one topic. Seems like it'd be a waste to separate them out.

  1. I understand for the most part pointers, and the operators themselves. (Although I am curious why the * is called the de-reference operator, since isn't it referring to a reference of what it contains?)

    I.e:

    int x = 25;
    int *p = &x;
    

    So & makes sense, since *p is a place in the stack of type int that contains the address of x (which we know is 25).

    So by saying *p are we 'referencing' the address of p which is pointing to the 25. Maybe it's just an English semantics thing? Maybe someone can explain why it's called 'de-reference'?

  2. Take this valid code sample:

    int *p = new int; 
    *p = 5 
    

    Simple enough, we're making a new place in the stack with the size of an int pointer (whatever that may be). p will contain some address that has a 5 for a value.

    Thing is, I haven't actually declared anything that's storing a 5, so what the heck is p pointing to? I mean, this does indeed work, but I never made a int x = 5 or anything like that, and gave the address to p to point to? How does it even know?

    p contains an address that points to something, but I never made that 'address' it's pointing to? Does the compiler just know to create another address somewhere else? (Sorry if this is a really stupid question.)

  3. I was reading on here about some memory leaks on another question:

    A *object1 = new A();
    

    pretending A is a class first of all. Someone was saying the object1 stores the value of A. Isn't that incorrect? Doesn't object1 store the address of what new A() created and is pointing to it?

    Because delete object1 deletes the pointer, which points to the new A() (but from the other question delete object1 would indeed be the correct syntax. So does that leave the new A() hanging out there? Or does it get automatically deleted? (As you can tell I'm a bit confused.)

    If delete object1 does indeed delete what the pointer is pointing to and not the pointer itself, doesn't this just leave a dangling pointer?

Biffen
  • 6,249
  • 6
  • 28
  • 36
msmith1114
  • 2,717
  • 3
  • 33
  • 84
  • `delete` goes balanced with `new` and isn't supposed to be used for pointers obtained not from `new`. It's completely unclear what you're asking/talking about. You probably have some serious misconceptions regarding dynamic memory management. – πάντα ῥεῖ Nov 02 '15 at 20:05

2 Answers2

2
  1. Here

    int x = 25;
    int *p = &x;
    

* is not the dereferencing operator. It's part of the type of p. It basically says that p is a pointer.

  1. Here

    int *p = new int; 
    *p = 5;
    

The key is new int. It dynamically creates an int and returns it's address, so p now points to that address. *p = 5 (btw, here * is the dereferencing operator) modifies the value -- of that dymanically allocated int -- to 5

  1. Indeed object1 holds the address of the newly created A. Since we're here we should clarify this: A is a (user defined) type. So it makes no sense to say that A has a value. Objects of type A have value.

delete p doesn't delete a pointer. It does 2 things:

  • Destroys an object created by a new-expression
  • Deallocates storage previously allocated by a matching operator new

The pointer isn't actually changed, i.e. it still points to the same address. Only now that address isn't allocated, i.e. can't be dereferenced anymore.

You can further refer to this SO answer - Static, automatic and dynamic storage duration to further understand objects, pointers, new/delete.

Community
  • 1
  • 1
bolov
  • 72,283
  • 15
  • 145
  • 224
  • So the 'new' keyword in #2 actually returns the address too (to store in p)....interesting. That explains how it knows! cool. – msmith1114 Nov 02 '15 at 20:13
1

1: I'm not sure what you're implying when you say "which we know is 25". The address of x is not 25, rather that is the value of x. You do not set the address of x in a statment like int x = 25

But to answer your question, p is a reference, which is to say its value is an address. Accessing the value stored at the address p requires the *p, which dereferences the pointer.

2: You have allocated memory for p; you executed a new. You have allocated 4 (or 8) bytes of memory for a new integer on the heap, so p is pointing to a newly allocated block of memory. Saying *p = 5; tells the compiler to set the value stored at that address to 5.

3: Your assumption is correct; object1 does not store the value of a new A, rather points to a block of memory equivalent in size to aninstance of an object of size A.

delete object1; Does not delete the pointer. The pointer is simply an integer on the stack, rather it gives back the allocated memory for that pointer back to the system. A as you knew it is deleted, but the pointer still exists, and using it at this point is undefined behavior. You are correct in assuming you have a dangling pointer now, that is why you should always set deleted pointers to NULL.

DeepDeadpool
  • 1,441
  • 12
  • 36
  • Sorry for #1 I indeed meant value. But that makes sense that p is actually a reference (trying to think in terms of you are storing the address). That makes sense. – msmith1114 Nov 02 '15 at 20:15