On May 7, 7:48 pm, Keith Thompson <ks...@mib.org> wrote:
> Chad <cdal...@gmail.com> writes:
> > On May 7, 1:59 pm, "Kleuskes & Moos" <kleu...@xs4all.nl> wrote:
> >> On May 7, 10:54 pm, "Bill Cunningham" <nos...@nspam.invalid> wrote:
>
> >> > I have seen person in another place talking about deleting pointers. I
> >> > know in C++ they have new but can that be done in C? All I am aware of is
> >> > the NULL pointer. Just thought I'd inquire.
>
> >> You can't delete a pointer, not even in C++. You can delete the object
> >> the pointer points to, though.
>
> >> C does not really have an equivalent to the C++ new-operator. You can
> >> use malloc() or calloc() to allocate memory, and free() to release it
> >> again, but unlike C++ malloc() and calloc() do not initialize
> >> anything. That's up to the programmer.
>
> > What happens if the scope of a pointer, which has automatic duration,
> > is confined to a function. And now let's say this function returns
> > what it's pointing to. Wouldn't the pointer itself cease to exist
> > once the function returns?
>
> If the pointer *object* has automatic storage duration, then it ceases
> to exist, but the *value* of that object (which is the address of some
> other object) remains valid as long as that object continues to exist.
>
Let's say I have a function that that adds a node at the front of a
linked list. Maybe something like the following...
Node *insert(Node *listp, Node *newp)
{
newp->next = listp;
return newp;
}
Wouldn't newp itself no longer exist outside of this function call? I
mean, if later on I did...
int main(void)
{
add = insert(add, newitem("foo"));
return 0;
}
newp itself would have no meaning in main().