0

For example:

struct A {
  char *p0;
  char *p1;
}

int main(int argc, char **argv){
   char a=0;
   char *b="W";
   char c[]=['L','O','L',0];

   struct A p[3];
   p[0].p0=&a;
   p[1].p0=b;
   p[2].p0=c;

   for(int i=0;i<3;i++){
     p[i].p1=malloc(10);
     strcpy(p[i].p1, p[i].p0);
   }


I've read elsewhere that pointer comparisons aren't safe unless the pointers being compared point to the same array, block of memory, or only 1 after the array. However, my professor had no problems asking us in class what

  1. p[0].p0 < p[0].p1
  2. p[1].p0 < p[1].p1
  3. p[2].p0 < p[2].p1

Evaluates to. The answer is 0, 1, and 0.

I am theorizing that p[0].p0<p[1].p1 evaluates to 0 because p[0].p0 points to the memory location of the char a, which is stored on the stack which has a higher memory address that the space on the heap that p[0].p1 points to. This also why I think the expression p[2].p0 < p[2].p1 evaluates to 0, because p[2].p0 stores the memory location of the first element in the char array char[] c which is on the stack whereas p[2].p1 points to some space allocated on the heap (and therefore a lower memory address).

However, p[1].p0 < p[1].p1 evaluates to 1 because when char * are declared pointing to string literals, the string literal is stored beneath the heap somewhere in the global/static variables and code section of the virtual memory space. Therefore the address it stores is below that of the address stored by p[1].p1 (which is the address of the space allocated on the heap).

I don't know, I can't think of any other explanation for

  1. The answers provided by my professor to the above 3 expressions
  2. The answers are confirmed by me running the 3 expressions on my machine

Conventionally I would've thought the answer is undefined or something. Is there some kind of exception to comparing pointers so long as they are stored in a struct? Otherwise why would my prof. pose this as a quiz/test question for us?

trincot
  • 317,000
  • 35
  • 244
  • 286
Shisui
  • 1,051
  • 1
  • 8
  • 23
  • See also the OP's earlier question _[How does pointer comparison work in C? It is oK to compare pointers that don't point to the same array?](https://stackoverflow.com/questions/59516346/how-does-pointer-comparison-work-in-c-is-it-ok-to-compare-pointers-that-dont-p)_ – Jonathan Leffler Dec 29 '19 at 02:55
  • There's no basis in the standard for any of the three comparisons producing any specific answer — they all invoke undefined behaviour by comparing pointers to unrelated material. The values in the `.p0` members are pointers to data on the 'stack' (though the C standard doesn't recognize the necessity for a stack), and those are usually in very high memory addresses. The values in the `.p1` members are from the 'heap' (though again the C standard doesn't recognize the necessity for a heap), and those are usually in medium high memory addresses. The comparisons would normally be consistent. – Jonathan Leffler Dec 29 '19 at 02:57
  • This is the same case as your prior question, as the contents of the `p0` and `p1` members do not point to members of the same object. Had you compared `&p[0].p0 < &p[0].p1` on the other hand that would be valid and the result would be 1 because member `p0` comes before member `p1`. – dbush Dec 29 '19 at 03:04
  • This code also invokes undefined behavior because `p[0].p0` does not point to a string but you pass it to `strcpy`. – dbush Dec 29 '19 at 03:07

0 Answers0