> Very true when the function returns the stack is emptied and all local
> variables are deleted. But the pointer is made on the heap not the
> stack!So you will have to "delete" the pointer manually. You are taking
> control away from the compiler and telling it that you are implementing
> it manually by making a pointer. So if there was a "Garbage Collector"
> this is what it would collect. Otherwise like in this case you have a
> memory leak.
    Do you mean a variable declared pointer is declared space on heap ??
    Well as per my information, when a pointer is created it being a
variable is created on stack (as it's a local variable). But the     if
memory is assigned to it through malloc (or through it's brother/sister)
the memory is allocated on heap. So the variable,     even though it's
declared a pointer will cease to exist once the function is returned - so no
memory leak in this case.
    Still I am not 100% sure about what I have said, but when I tried to
print the *address* of a pointer (local to a function),         and address
of other variable (not a pointer) I saw two addresses close to each other
(variable -> 0xbffff8dc & pointer ->     0xbffff8e0).

>
> > > So your code works only by accident. I'm surprised that gcc's warnings
> > > are defeated by your intermediate assignment (ptr = string), but only

> As far as gcc warning for ptr=string goes it is not complaining since
> string is an array and "string" actually contains the "memory address"
> of starting of the array. So since you are assigning address "string" to
> ptr it is not complaining and happily proceeding.
    Yes, I agree that ther should not be any warning at all. I got confused
because when one directly tries to return address of local variable, gcc
complains. I really mixed up two. thanx.


Still frnds, I am not very clear !!!

regards,
vikas




_______________________________________________
ilugd mailinglist -- [EMAIL PROTECTED]
http://frodo.hserus.net/mailman/listinfo/ilugd
Archives at: http://news.gmane.org/gmane.user-groups.linux.delhi 
http://www.mail-archive.com/[EMAIL PROTECTED]/

Reply via email to