473,395 Members | 1,341 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

amount of memory allocated to a pointer

how could i find out how much memory is blocked(or has been allocated
to a pointer)

consider,
int *p=new int;
or
int *p=new int[100];

suppose i dont know the right hand side of the statement i.e.
new int or new int[100] or new int[n] (where n is calculated during
runtime)

(definitely sizeof(p) would not give me the amount of memory
allocated.)

i would be interested in knowing the amount of memory taken up by the
respective pointers in a program. how could i possibly achieve this?
Are there any methods or standard routines to measure the amount of
memory?
If there are no standard methods or routines why would this be so ?

Sep 12 '07 #1
13 2491
pratap wrote:
how could i find out how much memory is blocked(or has been allocated
to a pointer)

consider,
int *p=new int;
or
int *p=new int[100];
This doesn't look like C to me. Did you mean to post to comp.lang.c++?
suppose i dont know the right hand side of the statement i.e.
new int or new int[100] or new int[n] (where n is calculated during
runtime)

(definitely sizeof(p) would not give me the amount of memory
allocated.)

i would be interested in knowing the amount of memory taken up by the
respective pointers in a program. how could i possibly achieve this?
Are there any methods or standard routines to measure the amount of
memory?
Not in C and I don't believe C++ is any different.
If there are no standard methods or routines why would this be so ?
I had a former colleague who used to say that he would answer "what" and
"how" questions, but not "why" questions...

Basically, the language simply doesn't work that way.
Sep 12 '07 #2
Mark Bluemel said:
pratap wrote:
<snip>
>i would be interested in knowing the amount of memory taken up by the
respective pointers in a program. how could i possibly achieve this?
Are there any methods or standard routines to measure the amount of
memory?

Not in C and I don't believe C++ is any different.
Actually, the amount of memory taken up by a pointer is very easy to
determine. In the quoted paragraph (above) the OP asks the wrong
question. What he ought to be asking is how, given a pointer to
dynamically allocated memory, how many objects of the proper type can
be stored in the memory thus allocated. And the answer is very easy:
when one allocates this memory in the first place, one knows how many
objects can be stored therein, so all one has to do is Not Forget.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 12 '07 #3
On Sep 12, 2:50 pm, Richard Heathfield <r...@see.sig.invalidwrote:
Mark Bluemel said:
pratap wrote:

<snip>
i would be interested in knowing the amount of memory taken up by the
respective pointers in a program. how could i possibly achieve this?
Are there any methods or standard routines to measure the amount of
memory?
Not in C and I don't believe C++ is any different.

Actually, the amount of memory taken up by a pointer is very easy to
determine. In the quoted paragraph (above) the OP asks the wrong
question. What he ought to be asking is how, given a pointer to
dynamically allocated memory, how many objects of the proper type can
be stored in the memory thus allocated. And the answer is very easy:
when one allocates this memory in the first place, one knows how many
objects can be stored therein, so all one has to do is Not Forget.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
one more way is using _msize(p)
this is even more easier than remembering! !-)

Sep 12 '07 #4
pratap wrote about getting the size of an allocated memory block:
one more way is using _msize(p)
this is even more easier than remembering! !-)
1) This is neither standard nor portable.
2) On implementations where this is available, it works with malloc'ed
blocks ONLY.
3) Even on these implementations, it DOESN'T work. It tends to give sizes
larger than the allocated size; Giving the effective size of allocation
(counting alignments), and not the requested size.
4) It transforms C in BASIC.
With typical C functions accepting a pointer memory block as parameter as
well as some size information, you can pass memory you've allocated from
anywhere, you can write your own memory pool or sub-heap of malloc's heap,
you can manually cut your memory blocks in pieces. Programmers are very
used to have the freedom of memory use.
Using _msize is removing this freedom.

Lazyiness has many disadvantages.

--
You can contact me at <ta*****************@yahoDELETETHATo.fr>
Sep 12 '07 #5
"pratap" <pr*********@gmail.comschrieb im Newsbeitrag
news:11**********************@d55g2000hsg.googlegr oups.com...
On Sep 12, 2:50 pm, Richard Heathfield <r...@see.sig.invalidwrote:
>Mark Bluemel said:
pratap wrote:

<snip>
>i would be interested in knowing the amount of memory taken up by the
respective pointers in a program. how could i possibly achieve this?
Are there any methods or standard routines to measure the amount of
memory?
Not in C and I don't believe C++ is any different.

Actually, the amount of memory taken up by a pointer is very easy to
determine. In the quoted paragraph (above) the OP asks the wrong
question. What he ought to be asking is how, given a pointer to
dynamically allocated memory, how many objects of the proper type can
be stored in the memory thus allocated. And the answer is very easy:
when one allocates this memory in the first place, one knows how many
objects can be stored therein, so all one has to do is Not Forget.

<snip>

one more way is using _msize(p)
this is even more easier than remembering! !-)
If your implementation happens to have that. It's non-standard.

Bye, Jojo
Sep 12 '07 #6
On Wed, 12 Sep 2007 10:01:45 +0000, pratap wrote:
pratap wrote:
>i would be interested in knowing the amount of memory taken up by
the
respective pointers in a program. how could i possibly achieve this?
Are there any methods or standard routines to measure the amount of
memory?
[snip]
one more way is using _msize(p)
this is even more easier than remembering! !-)
It is not standard C, and AFAIK not even standard C++. Probably it
is a compiler extension, but it won't work on others compilers. In
standard C the only way to know how big is the array containing the
object to which a pointer points is remembering it in the first
place.

BTW, you should snip parts of the post you're replying to which
are not relevant to your reply, and this almost always includes
signatures (the part which begins with "-- \n").
--
Army1987 (Replace "NOSPAM" with "email")
If you're sending e-mail from a Windows machine, turn off Microsoft's
stupid “Smart Quotes” feature. This is so you'll avoid sprinkling garbage
characters through your mail. -- Eric S. Raymond and Rick Moen

Sep 12 '07 #7
pratap wrote:
how could i find out how much memory is blocked(or has been allocated
to a pointer)

consider,
int *p=new int;
or
int *p=new int[100];

suppose i dont know the right hand side of the statement i.e.
new int or new int[100] or new int[n] (where n is calculated during
runtime)

(definitely sizeof(p) would not give me the amount of memory
allocated.)

i would be interested in knowing the amount of memory taken up by the
respective pointers in a program. how could i possibly achieve this?
Are there any methods or standard routines to measure the amount of
memory?
If there are no standard methods or routines why would this be so ?
If you use counted structures the problem disappears. Instead of
char ^pointers you use counted string and counted vectors.

typedef struct tagVector {
size_t len;
size_t elementSize;
void *data;
} VECTOR;

Then at any moment you know the size of your object. Some "malloc"
implementations use this structure for their blocks, returning just a
pointer to the data. Then, they can know at any moment the size of the
block. You can do the same.

This is easier than remembering the length associated with each block in
a notebook at your side...
Sep 12 '07 #8
pratap wrote:
Richard Heathfield <r...@see.sig.invalidwrote:
.... snip ...
>>
Actually, the amount of memory taken up by a pointer is very easy
to determine. In the quoted paragraph (above) the OP asks the
wrong question. What he ought to be asking is how, given a pointer
to dynamically allocated memory, how many objects of the proper
type can be stored in the memory thus allocated. And the answer is
very easy: when one allocates this memory in the first place, one
knows how many objects can be stored therein, so all one has to do
is Not Forget.

one more way is using _msize(p)
this is even more easier than remembering! !-)
A minor problem being that _msize() doesn't exist.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com

Sep 12 '07 #9
In article <46***************@yahoo.com>, CBFalconer
<cb********@yahoo.comwrites
>pratap wrote:
>Richard Heathfield <r...@see.sig.invalidwrote:
... snip ...
>>>
Actually, the amount of memory taken up by a pointer is very easy
to determine. In the quoted paragraph (above) the OP asks the
wrong question. What he ought to be asking is how, given a pointer
to dynamically allocated memory, how many objects of the proper
type can be stored in the memory thus allocated. And the answer is
very easy: when one allocates this memory in the first place, one
knows how many objects can be stored therein, so all one has to do
is Not Forget.

one more way is using _msize(p)
this is even more easier than remembering! !-)

A minor problem being that _msize() doesn't exist.
It quite plainly does exist because he is using it.

What you might mean is that _msize() is not part of the standard C
library. Which is far more helpful and may tell the OP something he did
not know.

--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ ch***@phaedsys.org www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

Sep 12 '07 #10
pratap wrote:
how could i find out how much memory is blocked(or has been allocated
to a pointer)

consider,
int *p=new int;
or
int *p=new int[100];
Consider that those lines are syntax errors in C. Perhaps you meant to
post to <news:comp.lang.c++where obscurantism is spoken.
I suspect the answer is the same in C++ as in C: if you want to know how
much space you have allocated, remember it. This is not one of those
cases where laziness pays.
Sep 12 '07 #11

"Martin Ambuhl" <ma*****@earthlink.netwrote in message
news:5k************@mid.individual.net...
pratap wrote:
>how could i find out how much memory is blocked(or has been allocated
to a pointer)

consider,
int *p=new int;
or
int *p=new int[100];

Consider that those lines are syntax errors in C. Perhaps you meant to
post to <news:comp.lang.c++where obscurantism is spoken.
I suspect the answer is the same in C++ as in C: if you want to know how
much space you have allocated, remember it. This is not one of those
cases where laziness pays.
Not really. In C++ approved style is to use container classes for all your
arrays and other structures. These have length / size methods. All the
memory management is done for you; it is seldom necessary to call new,
unless implementing your own custom container, and malloc() is there only
for compatibility with C, it shouldn't be used in straight C++.

(comp.lang.c++ added).

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Sep 13 '07 #12
On 2007-09-13 12:54, Malcolm McLean wrote:
"Martin Ambuhl" <ma*****@earthlink.netwrote in message
news:5k************@mid.individual.net...
>pratap wrote:
>>how could i find out how much memory is blocked(or has been allocated
to a pointer)

consider,
int *p=new int;
At least sizeof(int)
>>or
int *p=new int[100];
At least sizeof(int) * 100

The standard only guarantees that enough memory to contain the type is
allocated, it does not place any restrictions on allocating more. That
is up the the allocator.

--
Erik Wikström
Sep 13 '07 #13
Erik Wikström wrote:
Malcolm McLean wrote:
>"Martin Ambuhl" <ma*****@earthlink.netwrote in message
>>pratap wrote:

how could i find out how much memory is blocked(or has been
allocated to a pointer)

consider,
int *p=new int;

At least sizeof(int)
>>>or
int *p=new int[100];

At least sizeof(int) * 100

The standard only guarantees that enough memory to contain the
type is allocated, it does not place any restrictions on
allocating more. That is up the the allocator.
This is about C++. This newsgroup is c.l.c, and the discussion is
off-topic here. This is why crossposts between these newsgroups
should never be created. F'ups set.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Sep 13 '07 #14

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

Similar topics

2
by: hall | last post by:
I have a question regarding where memory is allocated when arrays are created. I'll illustrate this by example. I may be wrong on some details, do feel free to correct me. The code piece: int...
10
by: Jonathan Ames | last post by:
Moving to C++ from Java, I'm still confused by some aspects of memory cleanup operations. For example, let's say I have a class MovingObject which maintains a pointer to another class...
2
by: mosfets | last post by:
Hi, I'm having a little trouble figuring out the difference in terms of memory allocation between: class person_info; class A { private:
10
by: s.subbarayan | last post by:
Dear all, I happen to come across this exciting inspiring article regarding memory leaks in this website: http://www.embedded.com/story/OEG20020222S0026 In this article the author mentions:...
6
by: lovecreatesbeauty | last post by:
Hello experts, 1. Does C guarantee the data layout of the memory allocated by malloc function on the heap. I mean, for example, if I allocate a array of 100 elements of structure, can I always...
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
6
by: CANCER.0707 | last post by:
The problem statement is as follows Create a library that creates pools of memory of different sizes.For e.g. one pool of 32 byte size, another pool of 64 byte size and so on.Create an array of...
50
by: arunajob | last post by:
Hi all, If I have a piece of code something like this void main(void) { char * p1="abcdefghijklmn"; ............................................. }
6
by: Mahendra | last post by:
I have two cases - 1. When I have a pointer A pointing to a heap memory - What happens when I dereference the pointer A using free(A). It deallocated the heap memory the pointer was pointing...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.