HN user

je-so

8 karma
Posts0
Comments4
View on HN
No posts found.
C Puzzles 11 years ago

If you know that sizeof(float) >= sizeof(int) the memory alignment should be fine. Also in this case * (int* )&a does not access any bytes outside of an defined data object.

To predict the outcome one have to know the representation of float (IEEE 754) and the big/little endian issues which could be different between floats and ints.

printf("%d\n", a) does promote the value a to a double first (promotion rules) and then either the first or last part i(assuming sizeof(double) == 2*sizeof(int)) is shown depending on little or big endian architecture.

Another interesting thing to know about pointer and arrays is:

  #include <stdio.h>
  typedef int a_t[100];

  int main(void)
  {
     int  a[100];
     a_t* p = &a;

     // all printed pointer values are equal
     printf("%p %p %p %p\n", (void*)a, (void*)&a, (void*)p,  (void*)*p);
     return 0;
   }

(See http://pastebin.com/KypbginU)

// corrected overflow hget

  for (int k2=k,o=0; t[k&(SIZE-1)] && **t[k&(SIZE-1)] != k2 && o<SIZE; ++k,++o);
    return t+(k&(SIZE-1));
// hset now allows overwrite
  for (int (**a)[2] = hget(t, k); a && (*a || (*a=malloc(sizeof(**t)))); (**a)[0]=k,(**a)[1]=v,a=0);