How to know the memory size allocated to a pointer...

Posted on 2006-11-14
Last Modified: 2008-06-03
Is there any way to know that whether a pointer has been allocated or not ? What is the memory size allocated to a pointer ?

Thks so much.
Question by:Harry_Kewell
LVL 48

Accepted Solution

AlexFM earned 30 total points
ID: 17938287
Int 32-bits OS this is 4 bytes, in 64-bits OS this is 8 bytes. Pointer has always size which is enough to cover whole address space.

To know whether pointer is allocated or not, always initialize it to 0. When pointer is released, reset it to 0 again.

Assisted Solution

UrosVidojevic earned 30 total points
ID: 17939957
If you have pointer ptr;

MyClass *ptr;

Then the size of that pointer is

sizeof ptr



Unfortunatelly, there is no portable way to get the size of allocated memory block at which ptr points.

Expert Comment

ID: 17942700
The answer to your question depends on the type of data the pointer references. If the pointer is to a buffer, it's pretty much impossible to figure out how much memory is allocated,which is why many Windows API calls require that you pass not only a pointer to a buffer, but the size of the buffer.

What exactly are you trying to do?

Expert Comment

ID: 21427219
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I will leave the following recommendation for this question in the Cleanup Zone:
  Split: AlexFM {http:#17938287} & UrosVidojevic {http:#17939957}

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

Sean Durkin
Experts Exchange Cleanup Volunteer

Expert Comment

ID: 21449008
Forced accept.

EE Admin

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C language IDE – Compilers installation 14 67
IdTCPClient1->Disconnect(); not working 3 61
Dialogbox API leak? 18 81
c++, dynamic object by json 1 24
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now