Can't use "Delete" on pointers

This is a discussion on Can't use "Delete" on pointers within the C Programming forums, part of the General Programming Boards category; For some reason, I can't seem to use the "delete" key word to delete pointers. It claims that it is ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,668
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 09: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, 12:18 PM

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