FUZxxlMOD•

Your post got caught in our spam filter. I apologize for the inconvenience.

2
kloetzl•

The value of a pointer becomes indeterminate when the object it points to (or just past) reaches the end of its lifetime.

Does freeing an object "poison" all pointers to it? Dereferencing is out of the question, but comparison should still work, right?

e: LLVMs scan-build doesn't report a problem for the given example.

2
Rhomboid•

No, comparison is not allowed. If a pointer is dangling, you cannot access it in any way. You can't compare it, cast its value as an integer, dereference it, test it as a boolean, etc. Any access is invalid. The same rules apply for uninitialized pointers.

4
boredcircuits•

Comparing dangling pointers is something I've wanted to do before, but couldn't because it's undefined. Basically, I had a region of memory that could be resized with realloc, and a separate array of pointers into that memory. The problem came when trying to update this array when the memory gets resized (and potentially moved). The easy solution was to do some pointer arithmetic based on the old and new location in memory, but no matter how you slice it this will invoke undefined behavior. (The solution I settled on, in the end, was to store the offset in the array, not a direct pointer, so moving had no effect.)

2
Rhomboid•

I think if you really wanted to you could save a copy of the old pointer in the form of a uintptr_t and then cast the new return value after realloc() to uintptr_t and compare the two. This gets around the dangling pointer issue, and an optimizing compiler would probably see what you were trying to do and avoid storing the copy. Of course it's not a 100% solution since the ability to cast a pointer to an int and get a meaningful value is not guaranteed, only implementation-dependent. But that conveniently acts as an escape valve for all the weirdo segmented architectures and whatnot that the original issue was designed to avoid, and on all modern systems that matter, converting a pointer to an appropriately sized integer type ought to have solid semantics.

1
boredcircuits•

Just to make sure I follow you, this is approximately what I wanted to do:

int *memory = malloc(10 * sizeof(int));
int *array[2] = { &memory[5], &memory[7] };

int *old = memory;
memory = realloc(memory, 20 * sizeof(int));
for ( size_t i = 0; i < 2; i++ )
    array[i] = array[i] - old + memory; // Undefined behavior!

What you suggested takes care of the old pointer, but the realloc also caused everything within array to dangle as well. As you said it's not valid to cast a dangling pointer to uintptr_t, so that would have to happen before the realloc, which means creating a second buffer to store the temporary values beforehand (I was memory constrained, so that's a non-starter).

I guess another solution could have been to store everything in array as a uintptr_t instead, and cast back to the appropriate pointer type before accessing. That might have been acceptable.

1
OldWolf2•

I think if you really wanted to you could save a copy of the old pointer in the form of a uintptr_t and then cast the new return value after realloc() to uintptr_t and compare the two.

The recommended semantics under "Pointer provenance via integer types" in N2012 suggests that behaviour of uintptr_t obtained by casting a pointer should retain the same provenance of the original pointer.

Their survey also explicitly mentions the case of reallocing a block containing pointers to itself, and trying to adjust the pointers if the allocation moved; although the main paper doesn't appear to explicitly address that topic.

1
kloetzl•

Thanks a lot.

1
skeeto•

Related: The "const" qualifier in C (Chris Torek; Linus Torvalds). It's a discussion about why kfree() in the Linux kernel takes a const void * rather than just void *.

From a very obvious and very real caller perspective, "free()" really doesn't change the thing the pointer points to. It does something totally different: it makes the pointer itself invalid.

In other words, if you think that "kfree()" changed the thing you free'd, you're simply wrong. It did no such thing. The memory is 100% the same, it's just that you cannot access it any more, and if you try, you'll get somebody elses memory.

In other words, "kfree()" can be const.

1
OldWolf2•

Not really related; there the const applies to the pointed-to memory, but this thread is talking about the pointer itself.

0
skeeto•

The point of the discussion is that free() invalidates the pointer, not the memory it points to, making all other copies of the pointer dangling and invalid (hence why it's related). Since it doesn't affect the pointed-to memory, it's not subverting the const.

2
OldWolf2•

I would say that it does affect the pointed-to memory: that memory was previously valid but now invalid, therefore it is affected.

The motivation seems to be that it would be convenient to be able to write:

char const *ptr = malloc(10);
// ...
free(ptr);

which seems like a fair enough thing to do, but currently is not allowed by ISO C. Linus' comments that "anyone who thinks this is right doesn't understand the type system" are a bridge too far IMO.

1