-2

i am a newbie. like to understand why "p=&a" does not work. thank you.

  class A{
      int *p;
  public:
      A(int a){p=new int; p=&a;}
      ~A(){delete p;}
  };


  int main(void){
      A B(11);
  }
user10293779
  • 103
  • 6
  • 3
    1) Consider learning from a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) instead of coding randomly. 2) Do you understand what `&` (address-of operator), and `*` (indirection operator) does, and how does the behavior of those differ? 3) What is the meaning of creating a pointer to a local variable (which is destroyed at the end of scope), while, at the same time, overwriting the pointer you got from `new`, effectively, creating a memory leak? – Algirdas Preidžius Sep 13 '18 at 13:47
  • Because `a` is the constructor's argument. It is gone after the end of the constructor, the memory the pointer then points to will be junk (and `delete` will probably cause a crash) – hlt Sep 13 '18 at 13:47
  • For same reasons stated here https://stackoverflow.com/questions/52306425/why-is-destructor-hanging – StoryTeller - Unslander Monica Sep 13 '18 at 13:48
  • Depends what you mean by "does not work". The `p = &a` assigns `p` to be equal to the address of `a`. That has the effect of losing track of (aka leaking) the memory allocated with `new int`. Then the destructor does `delete p` where `p` points at something not created with operator `new`. The behaviour is then undefined. – Peter Sep 13 '18 at 13:48
  • @hlt "_and `delete` will probably cause a crash_" `delete`ing a pointer, that was not allocated with `new` is undefined behavior. One can, just as likely, not witness any side-effects, as one could witness a crash. – Algirdas Preidžius Sep 13 '18 at 13:51
  • Memory leak. You assign `p` to `new` memory, then you overwrite the value in the pointer to point to a temporary variable. – Thomas Matthews Sep 13 '18 at 14:21
  • As a newbie, I recommend doing whatever you can to avoid pointers. Prefer passing by reference, using `std::string` and `std::vector`, and the *smart pointers*. Use pointers as a last result (until you learn *everything* about them and create checklists to verify you haven't misused them). – Thomas Matthews Sep 13 '18 at 14:24

1 Answers1

5

A(int a){p=new int; p=&a;} first of all allocates an int to the pointer p, then secondly trashes that pointer value with the address of the temporary a.

So you end up with a dangling pointer and a memory leak! The behaviour of your destructor will be undefined.

*p = a is fine, since you are dereferencing p. Although that said, using bare pointers as class members causes problems with copying instances of your object. It's best avoided.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • the error (or crash) was caused by two different memory addresses between created by "new int" and "&a". if assigned a value to p (*p=a), its newly allocated address was unchanged. so "delete" and "new" address was matched. – user10293779 Sep 15 '18 at 14:42