Thread: Determining the number of objects allocated to a pointer...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    56

    Determining the number of objects allocated to a pointer...

    Is there an operator ( similar to sizeof() ) that will tell how many objects have been allocated in memory using "new" to a pointer? In other words:

    Code:
    int * foo = new int[50];
    int bar = some_operator(foo);
    
    // bar should be 50
    //(or 200 if it referred to bytes, assuming 4-byte ints)
    // Either of these would be helpful.
    The reason for this is that I am writing wrapper functions for "new" and "delete." It is easy to tell how many objects are being allocated (since that's a parameter to new[]), but I am trying to determine how to tell how many are being deleted with delete[].

    Also, "delete" is typically used to deallocate a single object, while "delete[]" deallocates an array. Is it harmful to call "delete[]" on a single object (not an array, or an array of one, if you will)? Along similar lines, is it acceptable to allocate with "new[1]" instead of just "new"?

  2. #2
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    Is there an operator ( similar to sizeof() ) that will tell how many objects have been allocated in memory using "new" to a pointer?
    No, you have to save the size yourself.
    Is it harmful to call "delete[]" on a single object (not an array, or an array of one, if you will)?
    If you use new[] then you have to use delete[], if you use new, then you have to use delete. If you mix and match, the computer is free to do anything at all.
    p.s. What the alphabet would look like without q and r.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  3. Static objects allocated with new
    By Mario F. in forum C++ Programming
    Replies: 12
    Last Post: 07-20-2007, 08:08 AM
  4. Replies: 8
    Last Post: 01-04-2006, 07:42 PM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM