473,326 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Pointer and Its memory

I have two cases -

1. When I have a pointer A pointing to a heap memory - What happens when
I dereference the pointer A using free(A).

It deallocated the heap memory the pointer was pointing to. Is that
correct ?

2. If I have a pointer A pointing to a heap memory. Now I create another
pointer B which points to same heap memory as pointer A. But now, I do
not want pointer A to still point to heap memory but I want to remove
the pointer A itself.

In this scenario, if I do not want to deallocate the created heap memory
pointed by pointer A (holding some data in the memory allocated) and
also do not want to copy the memory to some other same struct type, then
is there a way to remove pointer A pointing to heap memory but keep
pointer B pointing to the heap memory.

Essentially my question is - can i deallocate the memory holding the
pointer variable itself ? Can i use free()? If so, can i do it with
free(&p) by freeing the memory which holds the pointer variable itself.

Thanks
Mahendra
Oct 23 '08 #1
6 2620
>I have two cases -

Standard C does not define the term "heap". I'm using the definition
"that place from which malloc() & friends obtain memory".
>1. When I have a pointer A pointing to a heap memory - What happens when
I dereference the pointer A using free(A).
Calling free(A) does *NOT* necessarily dereference the pointer A.
(That depends on the internal workings of free(). It may simply
look for a matching entry in a table. ).
>It deallocated the heap memory the pointer was pointing to. Is that
correct ?
Correct.
>2. If I have a pointer A pointing to a heap memory. Now I create another
pointer B which points to same heap memory as pointer A. But now, I do
not want pointer A to still point to heap memory but I want to remove
the pointer A itself.
What does it mean to "remove the pointer"? Do you want to free()
it? You are not permitted to free() variables that were not allocated
with malloc() and friends.

You can assign NULL to pointer A so it no longer points into the
heap. You can assign something else to pointer A so it points
elsewhere (perhaps in the heap, perhaps not). If A is an auto
variable you can return from the function in which it is declared.

>In this scenario, if I do not want to deallocate the created heap memory
pointed by pointer A (holding some data in the memory allocated) and
also do not want to copy the memory to some other same struct type, then
is there a way to remove pointer A pointing to heap memory but keep
pointer B pointing to the heap memory.
There is no operation "remove a pointer". remove() applies to files.
If you wish a pointer to not point where it's pointing now, assign
it another value (NULL is one possibility).

>Essentially my question is - can i deallocate the memory holding the
pointer variable itself ?
Can i use free()?
If it was allocated with malloc() and friends, yes, you can free() it.
Otherwise you must not attempt it.
>If so, can i do it with
free(&p) by freeing the memory which holds the pointer variable itself.
If it's got a name, it was not allocated with malloc & friends, and
you must not free() it.
Oct 23 '08 #2
Richard Heathfield <r...@see.sig.invalidwrote:
...When you pass a pointer value to free(), one of three
things will happen.
Either:

(1) it is a value previously handed to you by malloc,
calloc, or realloc and not yet passed to free(), in
which case the memory pointed to will be deallocated; or
Note that realloc can deallocate memory too, if you ask
for zero size (in C89), in which case you have a null
pointer and point (2) applies if you pass it to free().
(2) it is a null pointer value, in which case nothing
will happen;
(3) it is some other value, in which case the behaviour
is undefined.
--
Peter
Oct 23 '08 #3
Peter Nilsson said:
Richard Heathfield <r...@see.sig.invalidwrote:
>...When you pass a pointer value to free(), one of three
things will happen.
Either:

(1) it is a value previously handed to you by malloc,
calloc, or realloc and not yet passed to free(), in
which case the memory pointed to will be deallocated; or

Note that realloc can deallocate memory too,
Yes. That's the trouble with trying to give a comprehensive answer in
finite time. :-)

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 23 '08 #4
Mahendra wrote:
I have two cases -

1. When I have a pointer A pointing to a heap memory - What happens when
I dereference the pointer A using free(A).
That is not dereferencing the pointer. That is deallocating the memory
it points at. free() itself may dereference a pointer based upon the
value you pass it, but your code does not dereference A.

Note that heap memory is an implementation detail, and not all systems
even have a heap. The key point is whether A points at the start of a
block of memory that was allocated by calling malloc(), calloc() or
realloc().
It deallocated the heap memory the pointer was pointing to. Is that
correct ?
Yes, it deallocated the memory (heap or not), that the pointer pointed at.
2. If I have a pointer A pointing to a heap memory. Now I create another
pointer B which points to same heap memory as pointer A. But now, I do
not want pointer A to still point to heap memory but I want to remove
the pointer A itself.

In this scenario, if I do not want to deallocate the created heap memory
pointed by pointer A (holding some data in the memory allocated) and
also do not want to copy the memory to some other same struct type, then
is there a way to remove pointer A pointing to heap memory but keep
pointer B pointing to the heap memory.

Essentially my question is - can i deallocate the memory holding the
pointer variable itself ?
You can only allocate objects with allocated storage duration, and a
named variable will always have either static or automatic storage
duration. However, when an object with automatic storage duration
reaches the end of its lifetime, the memory set aside to store that
object is no longer used for that purpose, and can be used for some
other purpose. That's similar in concept to deallocation. Would that
meet your needs? Here's an example:

void *B;
if(condition)
{
void *A = malloc(desired_size);
B = A;
}

At this point, the compiler is free to use the memory that used to be
used to store A, for some other purpose.

You could also dynamically allocate the memory used to store the
pointer, but in that case 'A' could not be the name used to refer to
that memory. Richard Heathfield has already given you an example of how
to do this.
Can i use free()?
You can only use free() to deallocate memory with allocated storage
duration.
... If so, can i do it with
free(&p) by freeing the memory which holds the pointer variable itself.
No, passing a pointer at free() which was not returned by one of the
malloc() family members has undefined behavior. It will most likely
cause your program to malfunction, possibly by aborting immediately,
possibly in a much more obscure fashion that you might not even notice
until after it has corrupted every record in a database containing data
that used to be worth millions of dollars.
Oct 23 '08 #5
"Mahendra" <im**@cc.gatech.eduwrote in message
news:gd**********@news-int.gatech.edu...

(snipped)
>
Essentially my question is - can i deallocate the memory holding the
pointer variable itself ? Can i use free()? If so, can i do it with
free(&p) by freeing the memory which holds the pointer variable itself.

Thanks
Mahendra
Why do you even want to do this?

Oct 23 '08 #6
In article <Hm*****************@nwrddc02.gnilink.net>, James Kuyper
<ja*********@verizon.netwrote:
Mahendra wrote:
[...]
Essentially my question is - can i deallocate the memory holding the
pointer variable itself ?

You can only allocate objects with allocated storage duration, and a
named variable will always have either static or automatic storage
duration. However, when an object with automatic storage duration
reaches the end of its lifetime, the memory set aside to store that
object is no longer used for that purpose, and can be used for some
other purpose. That's similar in concept to deallocation. Would that
meet your needs? Here's an example:

void *B;
if(condition)
{
void *A = malloc(desired_size);
B = A;
}

At this point, the compiler is free to use the memory that used to be
used to store A, for some other purpose.
Expanding on your example:

void *B;
if(condition)
{
void *A = malloc(desired_size);
printf( "%p\n", &A ); /* print address of A */
B = A;
}
void* C = B;
printf( "%p\n", &C ); /* print address of C */

Here, I've added a third variable C whose lifetime doesn't overlap that of
A, and printed the addresses of A and C. A compiler could print the same
address for both, indicating that it used the same memory for A and C.
Oct 23 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: __PPS__ | last post by:
Hello everybody in a quiz I had a question about dangling pointer: "What a dangling pointer is and the danger of using it" My answer was: "dangling pointer is a pointer that points to some...
52
by: Douglas Garstang | last post by:
I can't believe I've been trying to work this out for hours now, and I can't believe I couldn't find someone asking for a similar solution in the newsgroups. No wonder I hate C so much, and every...
22
by: Christopher Benson-Manica | last post by:
Is adding 0 to a pointer to non-void that is equal to NULL legal? int *p=NULL; p+=0; -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org ...
23
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when...
8
by: kathy | last post by:
If I have 2D array like: int **p; p = new int*; for(int i=0;i<10;i++) { p = new int; }
3
by: David Mathog | last post by:
This one is driving me slightly batty. The code in question is buried deep in somebody else's massive package but it boils down to this, two pointers are declared, the first is: char **resname...
27
by: Erik de Castro Lopo | last post by:
Hi all, The GNU C compiler allows a void pointer to be incremented and the behaviour is equivalent to incrementing a char pointer. Is this legal C99 or is this a GNU C extention? Thanks in...
5
by: mkaushik | last post by:
Hi everyone, Im just starting out with C++, and am curious to know how "delete <pointer>", knows about the number of memory locations to free. I read somewhere that delete frees up space...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.