deleting pointers?

  • Thread starter Bill Cunningham
  • Start date
B

Bill Cunningham

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
 
K

Kleuskes & Moos

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

rudolf

Kleuskes & Moos said:
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.
 
C

Chad

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?
 
K

Keith Thompson

Chad said:
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.
 
C

Chad

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

Shao Miller

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. :)
 
B

Barry Schwarz

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

Jorgen Grahn

On Sat, 7 May 2011 13:59:42 -0700 (PDT), "Kleuskes & Moos"


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
 
S

Shao Miller

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?
 
B

Bill Cunningham

Kleuskes said:
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
 
B

Bill Cunningham

Datesfat said:
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
 
B

Bill Cunningham

Bill said:
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
 
A

Angel

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

Chad

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

Chad

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

Barry Schwarz

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

Keith Thompson

Angel said:
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.
 
B

Bill Cunningham

Barry said:
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
 
B

Bill Cunningham

Barry said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Sizes of pointers 233
pointers and pointing 24
Pointers, question and fact confirmation please 12
Need Advice 10
Pointers 24
Looking For Advice 1
Deleting first N lines from a text file 26
Search multiple pages 7

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top