[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


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

Posted on 2006-11-14
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 48

Accepted Solution

AlexFM earned 120 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 120 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

649 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