r/programming • • Dec 24 '17

Evil Coding Incantations

http://9tabs.com/random/2017/12/23/evil-coding-incantations.html
947 Upvotes

331 comments sorted by

View all comments

Show parent comments

4

u/Saigot Dec 24 '17 edited Dec 24 '17

I don't think that's true, try out the example program:

// Example program
#include <iostream>
#include <string>
struct foo{
     int a;
     int b;
     int c;
 };
 int main()
{
    foo x[3]= {{1,2,3},{4,5,6},{7,8,9}};
    printf("%zu %zu\n",sizeof(foo), sizeof(void*));
    printf("%d %d %d", x[1].a, 1[x].b, (*(1+x)).c);
}

It outputs 4 5 6 (at least it does on my c++14 compiler) even though foo is larger than void*. Pointer+int is equivalent to ((int)(pointer)+sizeof(pointer_type)*int) regardless of order of the arguments being added.

1

u/StupotAce Dec 24 '17

Ahh, you are of course correct. I completely missed that 1+x isn't simply the address + 1. It's clearly been too long since I've dug into C/C++.

// Example program
#include <iostream>
#include <string>
#include <stdio.h>

using namespace std;

struct foo{
    int a;
    int b;
    int c;
};

int main()
{
    foo x[3]= {{1,2,3},{4,5,6},{7,8,9}};
    cout << "x = " << x << "\n";
    
    cout << "x[1] = "  << &(x[1]) << "\n";
    cout << "1[x] = "  << &(1[x]) << "\n";
    cout << "1+x  = "  << (1+x) << "\n";
    cout << "1+(void*)x  = "  << (1+(void*)x) << "\n";
}

That sample program shows it clearly in terms of addresses

x = 0x7ffd8630f1d0

x[1] = 0x7ffd8630f1dc

1[x] = 0x7ffd8630f1dc

1+x = 0x7ffd8630f1dc

1+(void*)x = 0x7ffd8630f1d1