473,473 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

determine pointer to point to array or single item during runtime

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 '06 #1
5 1813
ya************@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?
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
Jul 21 '06 #2
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
Jul 21 '06 #3
ya************@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
Jul 21 '06 #4
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:
ya************@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
Jul 21 '06 #5

ya************@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?
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

Jul 21 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Neal Coombes | last post by:
Posted to comp.lang.c++.moderated with little response. Hoping for better from the unmoderated groups: -------- Original Message -------- Subject: Return appropriately by value, (smart)...
5
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user...
40
by: Foobarius Frobinium | last post by:
Please review this guide for clarity, accuracy, etc. so I can hopefully compile a very good tutorial on how to use pointers in C, including advanced topics, that is easy to follow and exposes the...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
1
by: elziko | last post by:
My intention is to store an array of singles inside a DataTable so that it can me peristed somehow, maybe XML file, maybe Access/SQL Server I don't know yet so I'm just saving it as an XML file for...
27
by: Erik de Castro Lopo | last post by:
Hi all, The GNU C compiler allows a void pointer to be incremented and the behaviour is equivalent to incrementing a char pointer. Is this legal C99 or is this a GNU C extention? Thanks in...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
14
by: Szabolcs Borsanyi | last post by:
Deal all, The type typedef double ***tmp_tensor3; is meant to represent a three-dimensional array. For some reasons the standard array-of-array-of-array will not work in my case. Can I...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.