473,396 Members | 1,764 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,396 software developers and data experts.

Memory allocated for a given Pointer.

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
12 1738

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
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
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

"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
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
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
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
"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

"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
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
Thanks a lot. I will use std::vector
-Sabiyur -

Jul 4 '06 #12
"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 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...
30
by: jimjim | last post by:
Hello, This is a simple question for you all, I guess . int main(){ double *g= new double; *g = 9; delete g; cout<< sizeof(g)<<" "<<sizeof(double)<<" "<<sizeof(*g)<<" "<<*g<<" "<<endl; *g =...
74
by: ballpointpenthief | last post by:
If I have malloc()'ed a pointer and want to read from it as if it were an array, I need to know that I won't be reading past the last index. If this is a pointer to a pointer, a common technique...
62
by: ivan.leben | last post by:
How can I really delete a preloaded image from memory/disk cache? Let's say I preload an image by creating an Image object and setting its src attribute to desired URL: var img = new Image();...
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...
10
by: goose | last post by:
Hello all I've written a wrapper for malloc and friends. Its available from http://www.lelanthran.com/downloads/os_mem/index.php The reason for doing writing this so that newbies can...
21
by: llothar | last post by:
Hello, i need to manage a heap in shared memory. Does anybody know about a portable (win32+mac+posix) c implementation for this.
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
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,...

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.