Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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. …
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…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

707 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

12 Experts available now in Live!

Get 1:1 Help Now