Solved

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

Posted on 2006-11-14
6
1,602 Views
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.
0
Comment
Question by:Harry_Kewell
6 Comments
 
LVL 48

Accepted Solution

by:
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.
0
 
LVL 7

Assisted Solution

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

MyClass *ptr;

Then the size of that pointer is

sizeof ptr

or

sizeof(MyClass*).

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

Expert Comment

by:thoffman
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?
0
 
LVL 6

Expert Comment

by:SeanDurkin
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
0
 
LVL 1

Expert Comment

by:Computer101
ID: 21449008
Forced accept.

Computer101
EE Admin
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

770 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