Thread: Can't use "Delete" on pointers

  1. #1
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195

    Can't use "Delete" on pointers

    For some reason, I can't seem to use the "delete" key word to delete pointers. It claims that it is an undeclared function, which is odd. Anyone have any ideas why this might be? I am including stdio.h and a few others, but I can't think why it is doing this.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Because delete is a C++ thing.

    Use
    free( ptr );
    in C.
    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.

  3. #3
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    Quote Originally Posted by Salem View Post
    Because delete is a C++ thing.

    Use
    free( ptr );
    in C.
    Darn it. I knew I shouldn't have learnt C++ first. Thanks btw

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    How did you create the pointer?
    If you tried to use new it should have complained about the same thing.

    In C you use malloc() & free().
    In C++ you can still use malloc() & free() (but don't bother), there's the object oriented new & delete keywords.

    On the C++ side of things, you always need to pair up the right allocators/deallocators. i.e. new & delete, or new [] & delete [], or malloc() & free(). Never mix them, such as using new to allocate and free() to deallocate because you'll have big problems then.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  3. Question about using "delete" and pointers
    By kurtmax_0 in forum C++ Programming
    Replies: 4
    Last Post: 09-17-2003, 06:42 PM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM