By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,169 Members | 1,089 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,169 IT Pros & Developers. It's quick & easy.

Memory allocated for a given Pointer.

P: n/a
Hi All,
Is there any way to find out how much memory is allocated for a
given pointer.

For example:
int *p=new int[10];

I know, compiler keeps the information of how much bytes are allocated
for
the pointer "p" so that it can be released on delete.

But can we get this info by any way?
Thanks
Sabiyur

Jul 3 '06 #1
Share this Question
Share on Google+
12 Replies


P: n/a

Sabiyur wrote:
Hi All,
Is there any way to find out how much memory is allocated for a
given pointer.

For example:
int *p=new int[10];

I know, compiler keeps the information of how much bytes are allocated
for
the pointer "p" so that it can be released on delete.

But can we get this info by any way?
http://www.parashift.com/c++-faq-lit...html#faq-16.14

More importantly, see:

http://www.parashift.com/c++-faq-lit....html#faq-34.1

Cheers! --M

Jul 3 '06 #2

P: n/a
Sabiyur wrote:
int *p=new int[10];

I know, compiler keeps the information of how much bytes are allocated for
the pointer "p" so that it can be released on delete.

But can we get this info by any way?
This is probably a FAQ. The Standard C++ Library has no way. The
implication is you shouldn't throw the information away when you allocate
the array.

Use a std::vector for nearly any situation where you'd use a raw array.
Learn the vector first, and it will store its array size for you.

If you are in a pinch, _msize() or similar might work,
platform-specifically. Ask further such questions on a forum representing
your platform.

--
Phlip

Jul 3 '06 #3

P: n/a
Sabiyur posted:
Hi All,
Is there any way to find out how much memory is allocated for a
given pointer.

#include <cstddef>

int main()
{
int *p;

std::size_t amount_mem = sizeof p;
}

For example:
int *p=new int[10];

I know, compiler keeps the information of how much bytes are allocated
for
the pointer "p" so that it can be released on delete.

But can we get this info by any way?

Hi-jack "malloc" and "new" by writing your own. Store all the addresses
in some sort of look-up table.

--

Frederick Gotham
Jul 3 '06 #4

P: n/a

"Frederick Gotham" <fg*******@SPAM.comwrote in message
news:vt*******************@news.indigo.ie...
Sabiyur posted:
>Hi All,
Is there any way to find out how much memory is allocated for a
given pointer.


#include <cstddef>

int main()
{
int *p;

std::size_t amount_mem = sizeof p;
}
smart-ass! :-)

-H

Jul 3 '06 #5

P: n/a
Howard wrote:
"Frederick Gotham" <fg*******@SPAM.comwrote in message
news:vt*******************@news.indigo.ie...
Sabiyur posted:
Hi All,
Is there any way to find out how much memory is allocated for a
given pointer.

#include <cstddef>

int main()
{
int *p;

std::size_t amount_mem = sizeof p;
}

smart-ass! :-)

-H
Java has the ability to keep track of it using

int[] array = new int[size];
array.length;

in C++ you have two options.

1) Use boost::array<Type, size= { 1,2,3 };
It has the same memory layout as a normal c-array. the only problem is
that you are required to know size at compile time

so... that leads us to number two (which for some reason doesn't exist
in any proposal for the next standard. it is highly useful.

2) class Array<Type>
T* dat;
size_t size;
public:
Array(size_t length) ...
T& operator[](size_t i)

Jul 3 '06 #6

P: n/a
Goalie_Ca posted:
Howard wrote:
>"Frederick Gotham" <fg*******@SPAM.comwrote in message
news:vt*******************@news.indigo.ie...
Sabiyur posted:

Hi All,
Is there any way to find out how much memory is allocated for
a
>given pointer.
#include <cstddef>

int main()
{
int *p;

std::size_t amount_mem = sizeof p;
}

smart-ass! :-)

-H

Java has the ability to keep track of it using

int[] array = new int[size];
array.length;

You can achieve a lot of fancy stuff if you're willing to be disgustingly
inefficient. In fact, that was Java is brilliant at.

in C++ you have two options.

1) Use boost::array<Type, size= { 1,2,3 };
It has the same memory layout as a normal c-array. the only problem is
that you are required to know size at compile time

so... that leads us to number two (which for some reason doesn't exist
in any proposal for the next standard. it is highly useful.

2) class Array<Type>
T* dat;
size_t size;
public:
Array(size_t length) ...
T& operator[](size_t i)

There are about 358 different ways of doing this.

If you really wanted to be able to track the size of an array by a
pointer to its first element, then it wouldn't be impossible.
--

Frederick Gotham
Jul 3 '06 #7

P: n/a
Goalie_Ca wrote:
Howard wrote:
>>"Frederick Gotham" <fg*******@SPAM.comwrote in message
news:vt*******************@news.indigo.ie...
>>>Sabiyur posted:
Hi All,
Is there any way to find out how much memory is allocated for a
given pointer.
#include <cstddef>

int main()
{
int *p;

std::size_t amount_mem = sizeof p;
}

smart-ass! :-)

-H


Java has the ability to keep track of it using

int[] array = new int[size];
array.length;

in C++ you have two options.

1) Use boost::array<Type, size= { 1,2,3 };
It has the same memory layout as a normal c-array. the only problem is
that you are required to know size at compile time

so... that leads us to number two (which for some reason doesn't exist
in any proposal for the next standard. it is highly useful.

2) class Array<Type>
T* dat;
size_t size;
public:
Array(size_t length) ...
T& operator[](size_t i)
Maybe it isn't proposed because we use std::vector?

--
Ian Collins.
Jul 3 '06 #8

P: n/a
"Frederick Gotham" <fg*******@SPAM.comwrote:
Sabiyur posted:
Hi All,
Is there any way to find out how much memory is allocated for a
given pointer.


#include <cstddef>

int main()
{
int *p;

std::size_t amount_mem = sizeof p;
}
That doesn't work, my dear fellow. It always yields "4",
regardless of WHAT p points to. Why? Because p is a pointer,
and pointers are always 4 bytes on 32-bit comptuers (which
are about 99% of the computers in the world?).

If you did THIS:

int* p = new int[10];
cout << "Size of array is " << sizeof (*p) << endl;

Will it print 10? No, it prints 4, but this time for a
totally different reason. (*p) isn't an array of 10 ints;
it's the int that p points to! (That is, element 0 of the
dynamically allocated array.) Since ints are 4 bytes on 32-bit
computers, we again get "4".

There's just no way to do it.

Which is why it's better to use std::vector, std::list, or
std::deque for this kind of thing:

std::list<std::stringNames;
Names.push_back("Frederick Gotham");
Names.push_back("Robbie Hatley");
Names.push_back("Sabiyur");
Names.push_back("Ian Collins");
Names.push_back("Howard");
Names.push_back("Goalie_Ca");
cout << Names.size() << " people have responded to this thread" << endl;

Prints "6 people have responded to this thread".
Hi-jack "malloc" and "new" by writing your own. Store all the addresses
in some sort of look-up table.
Spoken like a true C programmer. You sound like my friend Ron,
the firmware guru. :-)

Me, I like standard containers better. You don't have to worry
about memory allocation (and deallocation) that way, and getting
current size is always as easy as "object.size()".

--
Cheers,
Robbie Hatley
Tustin, CA, USA
lonewolfintj at pacbell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
Jul 4 '06 #9

P: n/a

"Robbie Hatley" <bo***********@no.spamwrote in message
news:L3********************@newssvr13.news.prodigy .com...
"Frederick Gotham" <fg*******@SPAM.comwrote:
>Sabiyur posted:
Hi All,
Is there any way to find out how much memory is allocated for a
given pointer.


#include <cstddef>

int main()
{
int *p;

std::size_t amount_mem = sizeof p;
}

That doesn't work, my dear fellow. It always yields "4",
regardless of WHAT p points to. Why? Because p is a pointer,
and pointers are always 4 bytes on 32-bit comptuers (which
are about 99% of the computers in the world?).
He was responding to the question "How much memory is allocated for a given
pointer". And on 32-bit computers pointers are allocated 4 bytes of memory.
He then went on to answer what the OP actually MEANT to ask.
If you did THIS:

int* p = new int[10];
cout << "Size of array is " << sizeof (*p) << endl;

Will it print 10? No, it prints 4, but this time for a
totally different reason. (*p) isn't an array of 10 ints;
it's the int that p points to! (That is, element 0 of the
dynamically allocated array.) Since ints are 4 bytes on 32-bit
computers, we again get "4".

There's just no way to do it.

Which is why it's better to use std::vector, std::list, or
std::deque for this kind of thing:

std::list<std::stringNames;
Names.push_back("Frederick Gotham");
Names.push_back("Robbie Hatley");
Names.push_back("Sabiyur");
Names.push_back("Ian Collins");
Names.push_back("Howard");
Names.push_back("Goalie_Ca");
cout << Names.size() << " people have responded to this thread" << endl;

Prints "6 people have responded to this thread".
>Hi-jack "malloc" and "new" by writing your own. Store all the addresses
in some sort of look-up table.

Spoken like a true C programmer. You sound like my friend Ron,
the firmware guru. :-)

Me, I like standard containers better. You don't have to worry
about memory allocation (and deallocation) that way, and getting
current size is always as easy as "object.size()".

--
Cheers,
Robbie Hatley
Tustin, CA, USA
lonewolfintj at pacbell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/


Jul 4 '06 #10

P: n/a
Robbie Hatley posted:

>#include <cstddef>

int main()
{
int *p;

std::size_t amount_mem = sizeof p;
}

That doesn't work, my dear fellow. It always yields "4",
regardless of WHAT p points to. Why? Because p is a pointer,

Exactly.

and pointers are always 4 bytes on 32-bit comptuers (which
are about 99% of the computers in the world?).

Chapter and verse, please -- I am not aware of any such restriction
whereby a pointer must consume 4 bytes in memory on a 32-Bit system.

If you did THIS:

int* p = new int[10];
cout << "Size of array is " << sizeof (*p) << endl;

Will it print 10? No, it prints 4, but this time for a
totally different reason.

Indeed. Perhaps try this:
#include <iostream>

int main()
{

/* With pointers */
int (*p_array)[10] = new int[1][10];

std::cout << sizeof *p_array << '\n';
/* With references */
int (&array)[10] = *new int[1][10];

std::cout << sizeof array << '\n';
}
(*p) isn't an array of 10 ints;
it's the int that p points to! (That is, element 0 of the
dynamically allocated array.)

Indeed.

Since ints are 4 bytes on 32-bit
computers, we again get "4".

Indeed.

There's just no way to do it.

When determining the "way to do it", one must know the objective. Here's
a perfectly OK program:
int main()
{
int *p;

*p = 7;
}
It's OK because the objective is to demonstrate undefined behaviour.
--

Frederick Gotham
Jul 4 '06 #11

P: n/a
Thanks a lot. I will use std::vector
-Sabiyur -

Jul 4 '06 #12

P: n/a
"Frederick Gotham" wrote:
When determining the "way to do it", one must know the objective. Here's
a perfectly OK program:
int main()
{
int *p;

*p = 7;
}
It's OK because the objective is to demonstrate undefined behaviour.
Well, yes, I suppose it does do that. I see what you're
getting at, but it's best to mark such things with a disclaimer,
like so:

DISCLAIMER: Don't try this at home, folks! This may cause you
program to crash, or your operating system to crash, or your
hard disk to reformat itself, or perhaps your motherboard to
catch fire and burn up. Or it may start World War III. Who
knows? Wild pointers are dangerous beasts to taunt, and their
bites are often poisonous.

:-)

I had a dream a few years ago in which I was being chased by a wild
pointer. It was kinda spherical and hard and shiny, and it had a
mouth full of long, sharp, pointy teeth. It was chasing me and I
was trying to run away, but my legs wouldn't move fast enough.
I kept thinking, "It's not pointing to allocated memory! If it
catches me, it'll cause a general protection fault and crash the
system! Feet, do your stuff!"
--
Cheers,
Robbie Hatley
Tustin, CA, USA
lonewolfintj at pacbell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
Jul 5 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.