niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » C++
determine pointer to point to array or single item during runtime
Post new topic   Reply to topic Page 1 of 1 [6 Posts] View previous topic :: View next topic
Author Message
yancheng.cheok@gmail.com
*nix forums beginner


Joined: 05 May 2006
Posts: 4

PostPosted: Fri Jul 21, 2006 1:17 am    Post subject: determine pointer to point to array or single item during runtime Reply with quote

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.
Back to top
Victor Bazarov
*nix forums Guru


Joined: 07 Apr 2005
Posts: 3949

PostPosted: Fri Jul 21, 2006 1:26 am    Post subject: Re: determine pointer to point to array or single item during runtime Reply with quote

yancheng.cheok@gmail.com wrote:
Quote:
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.

Quote:
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.

Quote:

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
Back to top
Frederick Gotham
*nix forums Guru


Joined: 09 Jun 2006
Posts: 502

PostPosted: Fri Jul 21, 2006 3:29 am    Post subject: Re: determine pointer to point to array or single item during runtime Reply with quote

posted:

Quote:
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.


Quote:
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;
};


Quote:
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
Back to top
Julián Albo
*nix forums Guru Wannabe


Joined: 24 Feb 2005
Posts: 213

PostPosted: Fri Jul 21, 2006 6:39 am    Post subject: Re: determine pointer to point to array or single item during runtime Reply with quote

yancheng.cheok@gmail.com wrote:

Quote:
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
Back to top
sandy
*nix forums beginner


Joined: 07 May 2005
Posts: 40

PostPosted: Fri Jul 21, 2006 8:34 am    Post subject: Re: determine pointer to point to array or single item during runtime Reply with quote

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:
Quote:
yancheng.cheok@gmail.com 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
Back to top
peter.koch.larsen@gmail.c
*nix forums Guru Wannabe


Joined: 21 Jul 2005
Posts: 204

PostPosted: Fri Jul 21, 2006 11:47 am    Post subject: Re: determine pointer to point to array or single item during runtime Reply with quote

yancheng.cheok@gmail.com wrote:
Quote:
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.

Quote:

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.

Quote:

thank you.

/Peter
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [6 Posts] View previous topic :: View next topic
The time now is Sun Nov 23, 2008 2:51 pm | All times are GMT
navigation Forum index » Programming » C++
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Function Pointer Sikandar C 3 Fri Jul 21, 2006 1:23 pm
No new posts Oracle runtime Spitfire Server 0 Fri Jul 21, 2006 1:18 pm
No new posts database Share Memory Limit (2 GB ) in a Instance is tota... sadanjan@gmail.com IBM DB2 0 Fri Jul 21, 2006 12:57 pm
No new posts Single Bit Error Correction & Double Eror Detection using... Hari C 3 Fri Jul 21, 2006 11:43 am
No new posts Does stream I/O support "%a" floating-point format? John Friedland C++ 3 Fri Jul 21, 2006 9:26 am

Loans | Mortgage Calculator | Mortgage | Loans | Property Search
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.2593s ][ Queries: 16 (0.1490s) ][ GZIP on - Debug on ]