Can we say with certainty that the realloc result does not apply to the original pointer?

Let's say that we have:

char *a = malloc(sizeof(char*));    
char *b = realloc(a,sizeof(char*));

Is it possible to say with confidence that it is bnot an alias with a? The man page reallocsays that

The original ptr pointer is invalid, and any access to it is undefined (even if the redistribution was in place).

So, can I mark bas non-smoothing a, since we can no longer legally access a? However, this can lead to questionable optimization when to exclude the following branch:

if (a == b)
  something..

Based on my understanding, a comparison of itself a == bwould be UB, so is this technically correct optimization?

+4
source share
2

a .

n1570-§6.2.3 (p2):

[...] , undefined. , , ( ), .

, a == b undefined.

, , , , free.

:
1. null free?
2. .

+3

, a == b UB..

, .

"", haccks answer, free() d, , , , . , () , , undefined .

, , , , , . , , , .

,

, "b" "a"?

, , , , , C11, §7.22.3.5,

P2:

realloc , ptr, , size. , , . [...]

3:

[...] ptr - , realloc malloc size. , ptr , free realloc, undefined. , .

, P4:

realloc ( ) , .

+2

Source: https://habr.com/ru/post/1691341/


All Articles