[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

deleting pointers?

Bill Cunningham

5/7/2011 8:54:00 PM

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.

Bill


39 Answers

Kleuskes & Moos

5/7/2011 9:00:00 PM

0

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.
>
> Bill

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.

Datesfat Chicks

5/7/2011 10:20:00 PM

0

On Sat, 7 May 2011 13:59:42 -0700 (PDT), "Kleuskes & Moos"
<kleuske@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.
>>
>> Bill
>
>You can't delete a pointer, not even in C++. You can delete the object
>the pointer points to, though.

That is the most likely meaning -- deleting what the pointer points
to.

To add even more confusion, a pointer can point to dynamically
allocated memory containing one or more pointers, so in some sense you
can "delete" a pointer, but you can't delete the top-level pointer.

DFC

>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.

rudolf

5/8/2011 12:27:00 AM

0

In article
<f60a5169-ac65-4d00-a98e-49c1a8f090ed@28g2000yqu.googlegroups.com>,
"Kleuskes & Moos" <kleuske@xs4all.nl> wrote:

> 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.

calloc() initializes the memory to all zeros.

Chad

5/8/2011 12:46:00 AM

0

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.
>
> > Bill
>
> 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?

Keith Thompson

5/8/2011 2:49:00 AM

0

Chad <cdalten@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.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Chad

5/8/2011 3:11:00 AM

0

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().

Shao Miller

5/8/2011 5:26:00 AM

0

On 5/8/2011 10:59 PM, Bill Cunningham wrote:
> Barry Schwarz wrote:
>> How many times have people told you that to print a pointer value you
>> need to use %p and cast the pointer to void*?
>
> Never heard of such a thing. %p prints the address in memory that the
> pointer points to not what's stored there. I like the dereference method
> better anyway. I just don't always know when I need it.

The pointer value _is_ the "memory address" of the pointed-to object, if
I'm not mistaken.

In your earlier example, were you trying to print the pointer value or
the pointed-to object's value? For the former:

printf("%p\n", pointer);

For the latter:

printf("%d\n", *pointer);

Have a nice day. :)

Barry Schwarz

5/8/2011 5:26:00 AM

0

On Sat, 7 May 2011 13:59:42 -0700 (PDT), "Kleuskes & Moos"
<kleuske@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.
>>
>> Bill
>
>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.

You did not mean calloc() in the penultimate sentence since it does
initialize the entire area of allocated memory. Even realloc will
initialize the allocated memory as far as possible with the data in
the originally allocated area.

--
Remove del for email

Jorgen Grahn

5/8/2011 6:08:00 AM

0

On Sun, 2011-05-08, Barry Schwarz wrote:
> On Sat, 7 May 2011 13:59:42 -0700 (PDT), "Kleuskes & Moos"
> <kleuske@xs4all.nl> wrote:
....
>>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.
>
> You did not mean calloc() in the penultimate sentence since it does
> initialize the entire area of allocated memory. Even realloc will
> initialize the allocated memory as far as possible with the data in
> the originally allocated area.

Both calloc() and realloc() initialize yes, but not with anything
guaranteed to make sense. My struct Foo filled with zeros may not
make sense, and memmove() on it may not make sense either, if it e.g.
contains pointers into itself.

It's that lack of "type awareness" which C++ people don't like.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Shao Miller

5/8/2011 6:20:00 AM

0

On 5/9/2011 12:01 AM, Bill Cunningham wrote:
> Shao Miller wrote:
>
>> In your earlier example, were you trying to print the pointer value or
>> the pointed-to object's value? For the former:
>>
>> printf("%p\n", pointer);
>>

Oops, I forgot to cast your 'int *'-typed 'pointer' to 'void *'!
Because different pointer types can have different representations, this
is important!

>
> I meant the above but I usally use the less used %i rather than %d. It
> just started out that way. Avoid casting as much as possible.
>

Since both examples were "above," did you mean the second example; the
value of the pointed-to object?