Solved

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

Posted on 2006-11-14
6
1,605 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
thread-safe code in c++ 2 122
How to remove Recent Projects from Embarcadero C++ builder XE10. Berlin 2 78
Need some help with mailto 16 34
I could not set window to top 4 43
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 how to clear a vector as well as how to detect empty vectors in C++.

749 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