Ramprasad A Padmanabhan said:
Hello all,
If I assign one pointer to another , I had assumed I can call them
interchangably.
I can explain by the example below. I am doing a malloc to char* a which
is the same as char* b. But when I free(b) the memory allocated to a
does not get freed
Can someone explain this.
I'll try to; I'm going to ignore (for now) the fact that
your missed a header file and that "main ()" should be
"int main (void)" or "int main (int argc, char **argv)"
(or equivalent); I see that other posters have corrected
that and assume that you will follow their advice.
Also please note that in most C implementations, the
C programmer can not easily tell if a pointer is pointing
to valid memory (which was alloc()d in some way) or
invalid memory (memory which you cannot read and/or write
without nasty consequences). The implementation will also
not tell you which blocks of memory are available.
ok, we've got two pointers, and some memory; lets assume that
the following ascii diagram (you *are* using a fixed-width font,
right ?) represents the state of the machine at program startup.
in this machine of ours, we have only 200 "cells" of memory.
Pointers Memory available
cell_0 Y
cell_1 Y
cell_2 Y
cell_3 Y
...
cell_199 Y
when the program first starts up, we declare two pointers. now
remember that because we have not yet assigned them any values
(we have not made them point to anything), they could be pointing
/anywhere/. also remember that all those blocks of memory are
marked as "available". so after declaring the two pointers, our machine
now looks like this:
Pointers Memory available
cell_0 Y
cell_1 Y
a --> cell_2 Y
...
b --> cell_142 Y
...
cell_199 Y
like I said, they might be pointing anywhere. this means
that we should not try to read or write anything at the
memory they are pointing to because they might not be
pointing to a valid memory location (or "cell", in this
example). now we assign the value of pointer b to pointer
a, therefore a would be pointing to the same block of
memory as b. our machine is thus:
Pointers Memory available
cell_0 Y
cell_1 Y
cell_2 Y
...
a --> b --> cell_142 Y
...
cell_199 Y
in other words, they are both pointing to the same
bit of memory. now, we call malloc to *give* us
a chunk of memory that isn't being used by anyone
else, and we make b point to that chunk of memory
(which you wanted to be 100 "cells" wide).
so malloc looks through the memory cells, and decides
that since the memory from location 95 to 195 is not
being used, it gives us a /pointer/ to that, which we
assign to b. it *also* marks that memory as "not available".
machine looks like this after call to malloc:
Pointers Memory available
cell_0 Y
cell_1 Y
b --> cell_95 N
cell_96 N
...
a --> cell_142 N
...
cell_195 N
cell_196 Y
cell_197 Y
cell_198 Y
cell_199 Y
you see, malloc returned a pointer to
cell_95, and we assigned that pointer
to b, which is why b now points to
cell_95. unfortunately, we did not
reassign a to b, so a keeps its old
value (which was arbitrary to start with).
right, when we call free() with b as the argument,
free marks cell_95 - cell_195 as "available".
Pointers Memory available
cell_0 Y
cell_1 Y
b --> cell_95 Y
cell_96 Y
...
a --> cell_142 Y
...
cell_195 Y
cell_196 Y
cell_197 Y
cell_198 Y
cell_199 Y
so even though b and a are pointing *somewhere*,
we cannot store anything where they point, nor
can you read whatever it is that they are pointing
to. our pointers will almost certainly still be
pointing somewhere after we call free, but we may
not use a pointer which has been free()ed.
whew!
hth
goose,
implementations may differ, but thats all