Pointer problem

This is a discussion on Pointer problem within the C++ Programming forums, part of the General Programming Boards category; Hi, I want to understand why I get a runtime error with these lines: I try to delete a pointer ...

  1. #1
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114

    Pointer problem

    Hi, I want to understand why I get a runtime error with these lines: I try to delete a pointer that was never allocated somewhere (don't ask me why...) but I get an error telling me that the pointer is used without being defined...

    Code:
    int *a;
    delete a;
    a = new int;
    But then is there a way to check if a pointer allocated with NEW points somewhere or not?
    I tried using this, but still it says that the variable is used without being defined...

    Code:
    if (a != NULL) delete a;

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    a is not initialized, so it points to some unknown location. You cannot call delete with some unknown location that wasn't allocated with new. If you initialize your variable (always a good idea) to 0 or NULL, then it will work (i.e. the delete will not fail).

    There is no way to tell if a pointer points at "delete-able" memory or not other than to make sure your code uses the correct logic to remember that detail. One way is to always set pointers to null if they are not being used.

    This is why C++ has many tools available so that you don't have to manage dynamic memory yourself.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,438
    Code:
    a = new int();
    Will make sure your pointer is properly initialized when declared inside a function.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114
    Thanks, it worked!

    But what about "int()", is this some sort of constructor? In what way it makes sure it is initialized properly?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    It works like a constructor. The parentheses just mean to value-initialize the data stored in the location you just allocated space for. In the case of an int, it means initialize it to 0. You could put any integer into the parentheses and your new int would be initialized to that number.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > but I get an error telling me that the pointer is used without being defined...
    Because it appears "on the right" before it appears "on the left" (of an assignment)

    The compiler is smart enough to know that you never assigned a value to it, so it warns you that you're using it before you define it.
    Simple innit?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer to pointer realloc problem
    By prakash0104 in forum C Programming
    Replies: 14
    Last Post: 04-06-2009, 08:53 PM
  2. Another pointer problem
    By mikahell in forum C++ Programming
    Replies: 21
    Last Post: 07-20-2006, 07:37 PM
  3. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. pointer problem
    By DMaxJ in forum C Programming
    Replies: 4
    Last Post: 06-11-2003, 12:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21