memory allocated to a pointer

A

Ankit Raizada

Is there a way to know how much memory has being allocated(dynamically)
to a pointer. Related to this, can we find out has a pointer already
been freed? I am using VC++
 
C

Cong Wang

Ankit said:
Is there a way to know how much memory has being allocated(dynamically)
to a pointer. Related to this, can we find out has a pointer already
been freed? I am using VC++
It is you that allocate so much memory to a pointer,why don't you know
how much?
If you want to find out whether a pointer has already been freed,please
use a lint to check your C program.Your compiler can't do it.
 
J

John Bode

Ankit said:
Is there a way to know how much memory has being allocated(dynamically)
to a pointer. Related to this, can we find out has a pointer already
been freed? I am using VC++

There's no way to tell how much dynamic memory a pointer points to by
looking at the pointer itself. You have to keep track of that
information separately.

Similarly, there's no good way to tell if a pointer has been freed; the
convention I use is to set the pointer to NULL as soon as I free() it,
and use the following test every time I need to reference it:

if (pointer)
{
/* do stuff with pointer */
}

That doesn't help if I'm relying on someone else's library to do the
allocating and deallocating, though.
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
you can use the class CMemoryState for checking the memory and memory
leaking

Not in C, you can't.

There are no such things as "class"es in C, let alone a "class" called
"CMemoryState" in Standard (or non-standard) C

- --

Lew Pitcher, IT Specialist, Enterprise Data Systems
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFC8NP7agVFX4UWr64RArAgAJ4nMJ7L6JtxTtPIk0O321e7NNUVCwCeMfys
PjsekYO79WLepEJMarkQshM=
=9e2V
-----END PGP SIGNATURE-----
 
C

Christian Kandeler

Cong said:
It is you that allocate so much memory to a pointer,why don't you know
how much?
If you want to find out whether a pointer has already been freed,please
use a lint to check your C program.Your compiler can't do it.

If the compiler can't do it, lint can't do it either.


Christian
 
F

Flash Gordon

DarkTemp said:
you can use the class CMemoryState for checking the memory and memory
leaking

No you can't. Firstly C does not have classes, that is C++. Secondly, I
think you will find that CMemoryState is not part of C++, it might be an
extension available on some system, but it definitely is not portable
because it is not possible to do portably.

Please provide context for your post. Instructions on how to do this
with Google are posted regularly. Look at any of CBFalconer's posts and
you will see the instructions as he uses then as a sig.
 
R

Randy Howard

Ankit Raizada wrote
(in article
Is there a way to know how much memory has being allocated(dynamically)
to a pointer.

Not automagically. You can however wrap malloc() with your own
implementation that records this data somewhere in a method
convenient for you to look at later. This is typically not
really needed. What are you trying to accomplish by doing this?
Related to this, can we find out has a pointer already
been freed?

Again, wrap free() with your own macro or something that will
set the pointer to NULL after you call free. Then all you have
to do is the typical check for p == NULL, and you don't have to
worry about it unless you have something else corrupting memory.
I am using VC++

Sorry to hear that, but it doesn't change any of the above with
respect to standard C.
 
G

Gordon Burditt

Related to this, can we find out has a pointer already
Again, wrap free() with your own macro or something that will
set the pointer to NULL after you call free.

This hardly ever works in my experience. Why? Pointers are often
function arguments. Setting the argument to NULL does not set
the corresponding variable in the calling function to NULL.

If I am doing a lot of pointer manipulation, say, for linked lists,
I will have functions that allocate a node, put a node on a linked
list, move a node from one list to another, remove a node from a
linked list and free it, and free an entire list. The caller of
these functions won't have its copy of the node pointer set to NULL.
Then all you have
to do is the typical check for p == NULL, and you don't have to
worry about it unless you have something else corrupting memory.

It isn't that simple, unless you've taken great care to handle all
copies of the pointer. One way to do this is to pass around a
pointer to the list pointer. That gets rather awkward.

Gordon L. Burditt
 
R

Randy Howard

Gordon Burditt wrote
(in article said:
This hardly ever works in my experience. Why? Pointers are often
function arguments. Setting the argument to NULL does not set
the corresponding variable in the calling function to NULL.

Yes, but when the caller derefs the pointer by mistake later,
he'll get a nice crash right away instead of farther down the
road possible where it doesn't make sense.
 
G

Gordon Burditt

Related to this, can we find out has a pointer already
Yes, but when the caller derefs the pointer by mistake later,
he'll get a nice crash right away instead of farther down the
road possible where it doesn't make sense.

NO, he will *NOT* get a nice crash right away, since the caller's
copy of the pointer is NOT set to NULL. That's the point.

And since many people seem to think that this approach makes sure
that all the copies get set to null (and they don't), that makes
it all the harder to find the problem, because they think it
can't possibly be that problem, they would have had a nice crash
right away (at least on systems where dereferencing NULL does
give you an immediate crash) so it must be something else.

Gordon L. Burditt
 
E

Emmanuel Delahaye

Ankit Raizada wrote on 03/08/05 :
Is there a way to know how much memory has being allocated(dynamically)
to a pointer.

There is no 'memory allocated to a pointer'. This is misleading. All we
can have ia s a pointer containing anadress that can be the one of an
allocated block of memory.

If the only information we have is the address, there is no way to get
the size of the block. In most cases, you don't need it if the pointer
holding the address is correctly typed.

2 exceptions

- the pointer has the type void. No information about the size is
available at all.

- the pointer is typed, but the bloc is an array of this type. You know
the size of the first element (sizof *p), but you have no way to get
the number of elements.

This is why such a structure is recommended to hold allocated block
information.

struct allocated_T
{
size_t number_of_elements;
T *block_address;
};

or the generic way:

struct allocated
{
size_t size_of an_element;
size_t number_of_elements;
void *block_address;
};
Related to this, can we find out has a pointer already
been freed? I am using VC++

No. The Good Usage Rules recommend tu set the pointer to NULL after a
free()

free(p), p = NULL;

or some macro (yes, double evaluation, I know)

but it doesn't solve the alias problem. (The solution: don't alias
pointers...)

T *pa = malloc(...);
<...>
send_and_forget(pa), pa = NULL;

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

..sig under repair
 
E

Emmanuel Delahaye

R

Randy Howard

Gordon Burditt wrote
(in article said:
NO, he will *NOT* get a nice crash right away, since the caller's
copy of the pointer is NOT set to NULL. That's the point.

Duh, you're exactly right. my turn to wear the dunce cap.
 
C

Cong Wang

Christian said:
If the compiler can't do it, lint can't do it either.


Christian
No,you are wrong!Once I used a lint called splint on Linux to check the
memory leak problem and it did well!But the compiler can't do that.A
lint can give you more warnings than a compiler.Of course I say in C.
 
C

Christian Kandeler

Cong said:
> If you want to find out whether a pointer has already been freed,please
No,you are wrong!

NO YOU ARE!!!!111eleven
Once I used a lint called splint on Linux to check the
memory leak problem and it did well!

That is certainly possible.
But the compiler can't do that.

Yes, it can. It just chooses not to.
A lint can give you more warnings than a compiler.

lint as well as a compiler both have a static view of the program, which
limits their capabilities to diagnose possible errors. Now typically,
implementors of lint put more emphasis on looking for suspicious constructs
known to often cause trouble at runtime, but there is no technical reason
that would prevent a compiler from doing the same.


Christian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top