Re: question about pointers.

wongjoekmeu_at_yahoo.com
Date: 01/26/05


Date: 26 Jan 2005 07:44:28 -0800

Dear Karl Heinz Buchegger,

Thank you for your explanation. But if I understood you properly then
why does not the following code does not compile ?

----------

int *p = new int[5];
int *p = new int[10];

-----------
But when I write the following code here below which seems to be the
same for me that I redeclare the pointer to point again to somewhere
else, the compiler does not complain. WHy?? Do I have memory leak in
any of these cases ?
---------------
for (int i = 1; i<10; i++)
{
int *p = new int[i];
}
---------------

Robert

Karl Heinz Buchegger wrote:
> "wongjoekmeu@yahoo.com" wrote:
> >
> > Hello All,
> > I am learning C++ at the moment. Going through the book of SAM of
> > learning C++ in 21 days I have learned about pointers that it is
good
> > custome to always initialise them and to use delete before you try
to
> > give it a new address.
> > Can anyone explain to me why this code below which seems to voilate
> > this rule works ?
> >
> [snip]
>
> > So I initialised pCat to the null pointer and added the lines
between
> > line 28-29. And now the code does not work. The output on line 34
gives
> > only zeros. Can anyone explain me what I am doing wrong. To my
opinion
> > I am doing everything correct and according to the rules. But What
am I
> > doing wrong ??
>
> What the book did wrong: It obviously explained pointers and how to
> deal with them in a way you did not understand.
> What you did wrong: You don't understand pointers or to be more
exact:
> danymic memory allocation by now.
>
> Here is the deal:
> A pointer is a place in memory, just like any other variable that
holds
> the memory address of some other items in memory. As such it has a
name
> just like any other variable.
>
> So when you program does
>
> CAT * Family[500];
> CAT * pCat;
>
> you create 2 of those variables. Here they are:
>
> pCat Family
> +----------+ +------------+
> | | | |
> +----------+ +------------+
> | |
> +------------+
> | |
> +------------+
> | |
> . .
> . .
> . .
> | |
> +------------+
> | |
> +------------+
>
> Each of those rectangles contains a pointer. At the moment they
> are pointing to nowhere in particular, but that will change in
> a moment. When a pointer points to something, I will indicate
> that fact by placing an arrow in it. The arrow starts at the
> rectangle and ends at the thing 'pointed to'. Note: If I want
> to indicate that a pointer points to newhere, I simply write
> a 0 into the rectangle. Since none of the rectangles contain
> a 0 right now, I can conclude that nothing can be said about the
> state of the pointers right now.
>
> You program contines with
>
> pCat = new CAT;
>
> in the loop. OK lets perform the operation at i == 0
>
> new CAT That means that a CAT object is born somewhere in
memory ...
> pCat = new CAt ... and pCat gets a pointer to it
>
> pCat Family
> +----------+ +------------+
> | o | | |
> +----|-----+ +------------+
> | | |
> | +------------+
> | | |
> | +------------+
> | | |
> | . .
> | . .
> | . .
> | | |
> | +------------+
> | | |
> | +------------+
> |
> | +------------+
> +----------------------------------------->| Age: |
> +------------+
>
> Next:
> pCat->SetAge(2*i + 1);
>
> pCat Look up the rectangle called pCat
> pCat-> There must be an arrow originating from it
> Follow that arrow and you will get to an
> object of type CAT
> pCat->SetAge From that object, call the member function
setAge
> pCat->SetAge(2*i+1) and pass it the value 1 (remember, we ar ein
the first
> loop iteration, i has a value of 0)
>
> I guess that SetAge simply will set the Age field, thus
>
> pCat Family
> +----------+ +------------+
> | o | | |
> +----|-----+ +------------+
> | | |
> | +------------+
> | | |
> | +------------+
> | | |
> | . .
> | . .
> | . .
> | | |
> | +------------+
> | | |
> | +------------+
> |
> | +------------+
> +----------------------------------------->| Age: 1 |
> +------------+
>
> Next:
> Family[i] = pCat;
>
> Ok. In the array Family, look up the rectangle which corresponds to
> the 0-th entry. Make that entry point to the same object as pCat
>
> pCat Family
> +----------+ +------------+
> | o | | o----------------+
> +----|-----+ +------------+ |
> | | | |
> | +------------+ |
> | | | |
> | +------------+ |
> | | | |
> | . . |
> | . . |
> | . . |
> | | | |
> | +------------+ |
> | | | |
> | +------------+ |
> | v
> | +------------+
> +----------------------------------------->| Age: 1 |
> +------------+
>
> Loop finished, lets make another turn through the loop, for i == 1
>
> pCat = new CAT;
>
> pCat Family
> +----------+ +------------+
> | o | | o----------------+
> +----|-----+ +------------+ |
> | | | |
> | +------------+ |
> | | | |
> | +------------+ |
> | | | |
> | . . |
> | . . |
> | . . |
> | | | |
> | +------------+ |
> | | | |
> | +------------+ |
> | v
> | +------------+
> | | Age: 1 |
> | +------------+
> |
> |
> | +------------+
> +-------------------------------------->| Age: |
> +------------+
>
> pCat->SetAge( 2*i + 1);
>
> pCat Family
> +----------+ +------------+
> | o | | o----------------+
> +----|-----+ +------------+ |
> | | | |
> | +------------+ |
> | | | |
> | +------------+ |
> | | | |
> | . . |
> | . . |
> | . . |
> | | | |
> | +------------+ |
> | | | |
> | +------------+ |
> | v
> | +------------+
> | | Age: 1 |
> | +------------+
> |
> |
> | +------------+
> +-------------------------------------->| Age: 3 |
> +------------+
>
> Family[i] = pCat;
>
> pCat Family
> +----------+ +------------+
> | o | | o----------------+
> +----|-----+ +------------+ |
> | | o------------+ |
> | +------------+ | |
> | | | | |
> | +------------+ | |
> | | | | |
> | . . | |
> | . . | |
> | . . | |
> | | | | |
> | +------------+ | |
> | | | | |
> | +------------+ | |
> | | v
> | | +------------+
> | | | Age: 1 |
> | | +------------+
> | |
> | v
> | +------------+
> +------------------------------------->| Age: 3 |
> +------------+
>
> I guess you can now see what is going on:
> New CAT objects are allocated, a pointer to them is held in pCat.
> The object gets assigned some value and finaly the pointer in the
> Family array is made to point to that object. So in the end, the
> graphics would look like this: The family rectangle has pointers
> pointing all over the place to CAT object, each with an Age number
> set.
>
> Now the final loop in the original program simply walks through
> that array,
>
> for( i = 0; i < 500; i++ )
>
> follows each pointer
>
> Family[i]->
>
> tells the object at the end of the arrow to give its age
>
> Family[i]->GetAge()
>
> and outputs the number returned by the object
>
> cout << Family[i]->GetAge() << endl;
>
> So far so good.
> But then came you and modified the program. Lets see how this
influences
> the whole program:
>
> Again 2 Pointers are created:
>
> CAT * Family[500];
> CAT * pCat;
>
> pCat Family
> +----------+ +------------+
> | | | |
> +----------+ +------------+
> | |
> +------------+
> | |
> +------------+
> | |
> . .
> . .
> . .
> | |
> +------------+
> | |
> +------------+
>
> in the loop ( i == 0 )
>
> pCat = new CAT;
> pCat->SetAge( 2*i+1);
> Family[i] = pCat;
>
> pCat Family
> +----------+ +------------+
> | o | | o----------------+
> +----|-----+ +------------+ |
> | | | |
> | +------------+ |
> | | | |
> | +------------+ |
> | | | |
> | . . |
> | . . |
> | . . |
> | | | |
> | +------------+ |
> | | | |
> | +------------+ |
> | v
> | +------------+
> +----------------------------------------->| Age: 1 |
> +------------+
>
> Nothing new so far.
> But now it comes:
>
> delete pCat;
>
> Well. delete destroyes the object pCat points to. It wipes it out of
memory.
>
> pCat Family
> +----------+ +------------+
> | o | | o----------------+
> +----|-----+ +------------+ |
> | | | |
> | +------------+ |
> | | | |
> | +------------+ |
> | | | |
> | . . |
> | . . |
> | . . |
> | | | |
> | +------------+ |
> | | | |
> | +------------+ |
> | v
> |
> +----------------------------------------->
>
>
> pCat = 0;
>
> pCat Family
> +----------+ +------------+
> | 0 | | o----------------+
> +----------+ +------------+ |
> | | |
> +------------+ |
> | | |
> +------------+ |
> | | |
> . . |
> . . |
> . . |
> | | |
> +------------+ |
> | | |
> +------------+ |
> v
>
> Next run through the loop ( i == 1 )
>
> pCat = new CAT;
> pCat->SetAge( 2*i+1);
> Family[i] = pCat;
>
> pCat Family
> +----------+ +------------+
> | o | | o----------------+
> +----|-----+ +------------+ |
> | | o-----------+ |
> | +------------+ | |
> | | | | |
> | +------------+ | |
> | | | | |
> | . . | |
> | . . | |
> | . . | |
> | | | | |
> | +------------+ | |
> | | | | |
> | +------------+ | |
> | | v
> | |
> | |
> | v
> | +----------+
> +---------------------------------->| Age: 3 |
> +----------+
>
> and again:
>
> delete pCat;
> pCat = 0;
>
> pCat Family
> +----------+ +------------+
> | O | | o----------------+
> +----------+ +------------+ |
> | o-----------+ |
> +------------+ | |
> | | | |
> +------------+ | |
> | | | |
> . . | |
> . . | |
> . . | |
> | | | |
> +------------+ | |
> | | | |
> +------------+ | |
> | v
> |
> |
> v
>
>
> I think you can imagine, what this leads to in the end. Family
> still holds a lot of pointers. But there are no objects at the
> end of all those arrows. They have all been deleted.
>
> Well. Above I said: it has been wiped out of memory. Technically
> this is impossible. Memory is memory. There is still memory at
> all the end points of all those arrows and it is impossible for
> memory to not hold some value. But logically those objects are no
> longer there. The content of that memory is random and depends on a
> lot of things outside your control. This is why the loop in your
final
> program outputs 0. It could have operated otherwise also. You program
> could have simply crashed, because in the loop you take the pointer,
> the arrow, follow it and tell the object at the end of the arrow
> to give its age. But there is no object there any more!
>
>
> --
> Karl Heinz Buchegger
> kbuchegg@gascad.at



Relevant Pages

  • Re: Is this math test too easy?
    ... > communications glitch; one of the more laughable cartoons ... it was loaded into physical memory and, ... > Or one can interpret the character string as one of the values ... A pointer to an integer? ...
    (sci.math)
  • Re: grow list by tail, pointer example recipe -- please comment
    ... manufacturing a pointer with that address. ... the next cons cell. ... believe these lists are in consecutive memory locations. ...
    (comp.lang.lisp)
  • Re: some unanswered questions on C
    ... A pointer variable that's never been given a value. ... you don't know what memory you're modifying. ... >what i want to ask is that when i declare my buffer for fgets as ... "char *buffer" creates a pointer, ...
    (comp.unix.programmer)
  • Re: "Mastering C Pointers"....
    ... all means go ahead and dive right into the C language. ... Memory is a separate unit which just stores bits. ... A pointer at the hardware level _is an integer_. ... since loops make your logic more much ...
    (comp.lang.c)
  • Re: what is the purpose of C++ smart pointer
    ... pointer tracks the data it is referring to and updates itself ... following the changes of the memory it points to. ... How exactly will the smart pointer know that you moved the ... int * x = new int; ...
    (comp.os.linux.development.apps)