deleting pointers?

Discussion in 'C Programming' started by Bill Cunningham, May 7, 2011.

  1. 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
     
    Bill Cunningham, May 7, 2011
    #1
    1. Advertising

  2. On May 7, 10:54 pm, "Bill Cunningham" <> 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.
     
    Kleuskes & Moos, May 7, 2011
    #2
    1. Advertising

  3. Bill Cunningham

    rudolf Guest

    In article
    <>,
    "Kleuskes & Moos" <> 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.
     
    rudolf, May 8, 2011
    #3
  4. Bill Cunningham

    Chad Guest

    On May 7, 1:59 pm, "Kleuskes & Moos" <> wrote:
    > On May 7, 10:54 pm, "Bill Cunningham" <> 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?
     
    Chad, May 8, 2011
    #4
  5. Chad <> writes:
    > On May 7, 1:59 pm, "Kleuskes & Moos" <> wrote:
    >> On May 7, 10:54 pm, "Bill Cunningham" <> 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) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, May 8, 2011
    #5
  6. Bill Cunningham

    Chad Guest

    On May 7, 7:48 pm, Keith Thompson <> wrote:
    > Chad <> writes:
    > > On May 7, 1:59 pm, "Kleuskes & Moos" <> wrote:
    > >> On May 7, 10:54 pm, "Bill Cunningham" <> 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().
     
    Chad, May 8, 2011
    #6
  7. Bill Cunningham

    Shao Miller Guest

    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. :)
     
    Shao Miller, May 8, 2011
    #7
  8. On Sat, 7 May 2011 13:59:42 -0700 (PDT), "Kleuskes & Moos"
    <> wrote:

    >On May 7, 10:54 pm, "Bill Cunningham" <> 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
     
    Barry Schwarz, May 8, 2011
    #8
  9. Bill Cunningham

    Jorgen Grahn Guest

    On Sun, 2011-05-08, Barry Schwarz wrote:
    > On Sat, 7 May 2011 13:59:42 -0700 (PDT), "Kleuskes & Moos"
    > <> 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 .
     
    Jorgen Grahn, May 8, 2011
    #9
  10. Bill Cunningham

    Shao Miller Guest

    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?
     
    Shao Miller, May 8, 2011
    #10
  11. Kleuskes & Moos wrote:

    > You can't delete a pointer, not even in C++.


    I thought that sounded kind of funny. But the inquiry was made
    concerning c++ so I didn't know. That's why I was concerned with 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.
     
    Bill Cunningham, May 8, 2011
    #11
  12. Datesfat Chicks wrote:

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


    After doing this would you perhaps want to point the pointer to NULL
    after deleting the object?

    Bill
     
    Bill Cunningham, May 8, 2011
    #12
  13. Bill Cunningham wrote:
    > Datesfat Chicks wrote:
    >
    >>> 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.

    >
    > After doing this would you perhaps want to point the pointer to
    > NULL after deleting the object?


    If it's a char * of course. If an int * I guess initializing to
    something else might be appropriate.

    Bill
     
    Bill Cunningham, May 8, 2011
    #13
  14. Bill Cunningham

    Angel Guest

    On 2011-05-07, Bill Cunningham <> 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.


    A pointer is really just a big number, indicating a specific place in
    memory. "deleting a pointer" makes about as much sense as "deleting the
    number 2".

    You can delete or invalidate the object the pointer points to though.
    For dynamically allocated objects (i.e. with malloc() or friends in C,
    or with the "new" operator in C++), you can invalidate the object by
    releasing the allocated memory (i.e. with free() in C, "delete" in C++).
    Pointers may also become invalid if they point to an automatic object
    which ceases to exist.

    An invalidated pointer still holds the number it held before, but trying
    to access the object leads to undefined behaviour. It may access the old
    object, if nothing has overwritten it yet. It may access random data. Or
    it may crash your program.

    It is a good idea, in C, to assign the value NULL to a pointer
    variable after using free() on it. In that case, if the program still
    accidentally dereferences the pointer, it will stop with a null pointer
    derefence error instead of meddling with data it should not meddle with.
    This doesn't prevent all possible errors though.


    --
    The perfected state of a spam server is a smoking crater.
    - The Crater Corollary to Rule #4
     
    Angel, May 8, 2011
    #14
  15. Bill Cunningham

    Chad Guest

    On May 7, 8:10 pm, Chad <> wrote:
    > On May 7, 7:48 pm, Keith Thompson <> wrote:
    >
    >
    >
    >
    >
    > > Chad <> writes:
    > > > On May 7, 1:59 pm, "Kleuskes & Moos" <> wrote:
    > > >> On May 7, 10:54 pm, "Bill Cunningham" <> wrote:

    >
    > > >> >     I have seen person in another place talking about deletingpointers. 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().


    Never mind. I misread your post.
     
    Chad, May 8, 2011
    #15
  16. Bill Cunningham

    Chad Guest

    On May 7, 8:10 pm, Chad <> wrote:
    > On May 7, 7:48 pm, Keith Thompson <> wrote:
    >
    >
    >
    >
    >
    > > Chad <> writes:
    > > > On May 7, 1:59 pm, "Kleuskes & Moos" <> wrote:
    > > >> On May 7, 10:54 pm, "Bill Cunningham" <> wrote:

    >
    > > >> >     I have seen person in another place talking about deletingpointers. 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().


    Never mind. I misread your response.
     
    Chad, May 8, 2011
    #16
  17. On Sun, 8 May 2011 04:19:44 -0400, "Bill Cunningham"
    <> wrote:

    >Bill Cunningham wrote:
    >> Datesfat Chicks wrote:
    >>
    >>>> 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.

    >>
    >> After doing this would you perhaps want to point the pointer to
    >> NULL after deleting the object?

    >
    > If it's a char * of course. If an int * I guess initializing to
    >something else might be appropriate.


    After years of people begging you not to guess...

    Why do you think the type of pointer might have any relevance whether
    it should be set to NULL after the memory it points to is freed? Don't
    tell why you think it should or should not be set to NULL. Explain why
    the answer is different for different kinds of pointers.

    --
    Remove del for email
     
    Barry Schwarz, May 8, 2011
    #17
  18. Angel <> writes:
    > On 2011-05-07, Bill Cunningham <> 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.

    >
    > A pointer is really just a big number, indicating a specific place in
    > memory. "deleting a pointer" makes about as much sense as "deleting the
    > number 2".


    Sort of, but thinking of pointers as numbers can lead to some dangerous
    misconceptions. A pointer value is a location in memory. On some
    systems, such a location is represented by a number, or something that
    looks very much like a number, but the C language doesn't assume such a
    representation.

    Yes, you can perform arithmetic on pointers, but only in limited ways.

    Think about a street address, like "1600 Pennsylvania Avenue". It
    includes a number, 1600, but the address as a whole certainly isn't a
    number.

    [...]

    > It is a good idea, in C, to assign the value NULL to a pointer
    > variable after using free() on it. In that case, if the program still
    > accidentally dereferences the pointer, it will stop with a null pointer
    > derefence error instead of meddling with data it should not meddle with.
    > This doesn't prevent all possible errors though.


    Right. Most systems will trap in some way on attempts to dereference a
    null pointer, but the language doesn't guarantee it; instead, it merely
    says the behavior is undefined.

    On the other hand, setting a pointer to NULL does give it a value that
    can safely be examined.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, May 8, 2011
    #18
  19. Barry Schwarz wrote:

    > After years of people begging you not to guess...


    I've never written big programs before Barry so I have to guess at what
    messing with pointers that are no longer needed could do.

    > Why do you think the type of pointer might have any relevance whether
    > it should be set to NULL after the memory it points to is freed? Don't
    > tell why you think it should or should not be set to NULL. Explain why
    > the answer is different for different kinds of pointers.


    I've gotten errors with setting anything other than a char* to NULL. I
    would have to look up what the value of NULL is or simply as someone who
    knows off the top of their head what the standard says. Now I can't remember
    what they were so I have this idea that NULL is for char* pointers. Of
    course please correct me if I'm wrong.

    Bill
     
    Bill Cunningham, May 8, 2011
    #19
  20. Barry Schwarz wrote:

    > After years of people begging you not to guess...
    >
    > Why do you think the type of pointer might have any relevance whether
    > it should be set to NULL after the memory it points to is freed? Don't
    > tell why you think it should or should not be set to NULL. Explain why
    > the answer is different for different kinds of pointers.


    Ok I decided to test something and here it is:

    #include <stdio.h>

    int main()
    {
    int a = 50;
    int *pointer = &a;
    printf("%i\n", pointer);
    pointer = NULL;
    printf("%i\n", pointer);
    return 0;
    }

    And the result was this:
    -1073742988
    0

    So (I am not going to say I guess) pointer must be uninitialized and the
    NULL working with an int* I didn't expect that one. I expected NULL to bring
    up an error and pointer the int* to point to the address of where the OS has
    stored a's value being 50.

    Bill
     
    Bill Cunningham, May 8, 2011
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Min
    Replies:
    6
    Views:
    425
    Ron Samuel Klatchko
    Jul 1, 2003
  2. Xamalek
    Replies:
    7
    Views:
    708
  3. Harry Barker
    Replies:
    2
    Views:
    544
    Alf P. Steinbach
    Apr 19, 2006
  4. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    731
  5. crea
    Replies:
    2
    Views:
    441
    Nobody
    Dec 28, 2012
Loading...

Share This Page