determine pointer to point to array or single item during runtime

Discussion in 'C++' started by yancheng.cheok@gmail.com, Jul 21, 2006.

  1. Guest

    hello, may i know how i can determine whether a pointer is pointing to
    an array or a single item during runtime?

    this is because in certain situation, i need to determine whether to
    use delete or delete[] to deallocate the item(s) ponting by the
    pointer.

    also, is there any way to determine whether the allocated memory
    pointed by the pointer is allocated through new or c version calloc/
    malloc?

    thank you.
    , Jul 21, 2006
    #1
    1. Advertising

  2. wrote:
    > hello, may i know how i can determine whether a pointer is pointing to
    > an array or a single item during runtime?


    There is no portable way.

    > this is because in certain situation, i need to determine whether to
    > use delete or delete[] to deallocate the item(s) ponting by the
    > pointer.


    If it's you who allocated it, you know how to delete it. If it's not
    you who allocated it, you shouldn't be responsible for deletion.

    >
    > also, is there any way to determine whether the allocated memory
    > pointed by the pointer is allocated through new or c version calloc/
    > malloc?


    No, not portably. In most cases the developers decide to only use
    one method. I say, use 'new' and don't concern youself with anything
    else.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jul 21, 2006
    #2
    1. Advertising

  3. posted:

    > hello, may i know how i can determine whether a pointer is pointing to
    > an array or a single item during runtime?



    The language provides no such facility built-in.

    You would have to keep track of it yourself.


    > this is because in certain situation, i need to determine whether to
    > use delete or delete[] to deallocate the item(s) ponting by the
    > pointer.



    A look-up table of some sort. Or perhaps, instead of a pointer:

    template <class T>
    struct DynmPtr {

    T *const p;

    bool is_array;
    };


    > also, is there any way to determine whether the allocated memory
    > pointed by the pointer is allocated through new or c version calloc/
    > malloc?



    The language provides no such facility.

    You would have to keep track of it yourself.


    --

    Frederick Gotham
    Frederick Gotham, Jul 21, 2006
    #3
  4. wrote:

    > hello, may i know how i can determine whether a pointer is pointing to
    > an array or a single item during runtime?
    >
    > this is because in certain situation, i need to determine whether to
    > use delete or delete[] to deallocate the item(s) ponting by the
    > pointer.


    You can avoid the need to determine that by allocating an array of one
    element when you want a single item and always using delete [].

    --
    Salu2
    =?ISO-8859-15?Q?Juli=E1n?= Albo, Jul 21, 2006
    #4
  5. sandy Guest

    some compilers internally call malloc when u use "new" to allocate
    memory.
    I dont think u might be able to detect whether memory is allocated
    using new or malloc during run time.

    --
    sandeep nitta

    Julián Albo wrote:
    > wrote:
    >
    > > hello, may i know how i can determine whether a pointer is pointing to
    > > an array or a single item during runtime?
    > >
    > > this is because in certain situation, i need to determine whether to
    > > use delete or delete[] to deallocate the item(s) ponting by the
    > > pointer.

    >
    > You can avoid the need to determine that by allocating an array of one
    > element when you want a single item and always using delete [].
    >
    > --
    > Salu2
    sandy, Jul 21, 2006
    #5
  6. peter koch Guest

    wrote:
    > hello, may i know how i can determine whether a pointer is pointing to
    > an array or a single item during runtime?


    You can't do so in the general case, but if you stick to a single
    healthy rule, your problem is easily solved: simply don't use new[].
    new[] really is not needed as there is a far better substitute using
    std::vector.

    >
    > this is because in certain situation, i need to determine whether to
    > use delete or delete[] to deallocate the item(s) ponting by the
    > pointer.
    >
    > also, is there any way to determine whether the allocated memory
    > pointed by the pointer is allocated through new or c version calloc/
    > malloc?


    Nope. new could easily just call malloc.

    >
    > thank you.


    /Peter
    peter koch, Jul 21, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff
    Replies:
    2
    Views:
    313
    clintonG
    Mar 9, 2007
  2. Howard
    Replies:
    3
    Views:
    264
    Howard
    Dec 5, 2007
  3. Ratko
    Replies:
    7
    Views:
    278
    Reedick, Andrew
    Jul 17, 2008
  4. QUASAR
    Replies:
    6
    Views:
    409
    QUASAR
    Jan 17, 2004
  5. Saraswati lakki
    Replies:
    0
    Views:
    1,298
    Saraswati lakki
    Jan 6, 2012
Loading...

Share This Page