Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > determine pointer to point to array or single item during runtime

Reply
Thread Tools

determine pointer to point to array or single item during runtime

 
 
yancheng.cheok@gmail.com
Guest
Posts: n/a
 
      07-21-2006
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.

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-21-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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


 
Reply With Quote
 
 
 
 
Frederick Gotham
Guest
Posts: n/a
 
      07-21-2006
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
 
Reply With Quote
 
=?ISO-8859-15?Q?Juli=E1n?= Albo
Guest
Posts: n/a
 
      07-21-2006
(E-Mail Removed) 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
 
Reply With Quote
 
sandy
Guest
Posts: n/a
 
      07-21-2006
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:
> (E-Mail Removed) 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


 
Reply With Quote
 
peter koch
Guest
Posts: n/a
 
      07-21-2006

(E-Mail Removed) 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Share-Point-2010 ,Share-Point -2010 Training , Share-point-2010Hyderabad , Share-point-2010 Institute Saraswati lakki ASP .Net 0 01-06-2012 06:39 AM
pointer to an array vs pointer to pointer subramanian100in@yahoo.com, India C Programming 5 09-23-2011 10:28 AM
properly delete item during "for item in..." Ratko Python 7 07-17-2008 07:56 PM
determine if item is in array, how?? Jeff ASP .Net 2 03-09-2007 04:39 PM
Change style of a single row of the item list of datagrid, based on a field value of current item... QUASAR ASP .Net Datagrid Control 6 01-17-2004 07:46 PM



Advertisments