Memory allocated for a given Pointer.

S

Sabiyur

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
 
M

mlimber

P

Phlip

Sabiyur said:
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.
 
F

Frederick Gotham

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

Goalie_Ca

Howard said:
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)
 
F

Frederick Gotham

Goalie_Ca posted:
Howard said:
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.
 
I

Ian Collins

Goalie_Ca said:
Howard said:
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?
 
R

Robbie Hatley

Frederick Gotham said:
Sabiyur 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,
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::string> Names;
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/
 
J

Jim Langston

Robbie Hatley said:
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::string> Names;
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/
 
F

Frederick Gotham

Robbie Hatley posted:

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

Robbie Hatley

Frederick Gotham said:
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/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top