Skip to content

Instantly share code, notes, and snippets.

@bebehei
Created November 13, 2018 15:14
Show Gist options
  • Save bebehei/1f8b0fe97c4f13e01379a680ce6686c3 to your computer and use it in GitHub Desktop.
Save bebehei/1f8b0fe97c4f13e01379a680ce6686c3 to your computer and use it in GitHub Desktop.
stringtest to check the optimisation of constant substrings
[bebe:~/stringtest] % gcc --version
gcc (GCC) 8.2.1 20180831
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
[bebe:~/stringtest] % gcc -o stringtest stringtest.c -O0
[bebe:~/stringtest] % ./stringtest
sameptr: (diff: 0)
same1: 0x563efba8d004
same2: 0x563efba8d004
abptr: (diff: -5)
a: 0x563efba8d037
b: 0x563efba8d03c
digsptr: (diff: -5)
digs1: 0x563efba8d066
digs2: 0x563efba8d06b
[bebe:~/stringtest] % gcc -o stringtest stringtest.c -O1
[bebe:~/stringtest] % ./stringtest
sameptr: (diff: 0)
same1: 0x55ebae94101a
same2: 0x55ebae94101a
abptr: (diff: 1)
a: 0x55ebae941038
b: 0x55ebae941037
digsptr: (diff: -1)
digs1: 0x55ebae941061
digs2: 0x55ebae941062
[bebe:~/stringtest] % gcc -o stringtest stringtest.c -O2
[bebe:~/stringtest] % ./stringtest
sameptr: (diff: 0)
same1: 0x556c9d83601a
same2: 0x556c9d83601a
abptr: (diff: 1)
a: 0x556c9d836038
b: 0x556c9d836037
digsptr: (diff: -1)
digs1: 0x556c9d836061
digs2: 0x556c9d836062
[bebe:~/stringtest] % gcc -o stringtest stringtest.c -O3
[bebe:~/stringtest] % ./stringtest
sameptr: (diff: 0)
same1: 0x55dc269a301a
same2: 0x55dc269a301a
abptr: (diff: 1)
a: 0x55dc269a3038
b: 0x55dc269a3037
digsptr: (diff: -1)
digs1: 0x55dc269a3061
digs2: 0x55dc269a3062
[bebe:~/stringtest] % gcc -o stringtest stringtest.c -Os
[bebe:~/stringtest] % ./stringtest
sameptr: (diff: 0)
same1: 0x55d7f292f01a
same2: 0x55d7f292f01a
abptr: (diff: 1)
a: 0x55d7f292f038
b: 0x55d7f292f037
digsptr: (diff: -1)
digs1: 0x55d7f292f061
digs2: 0x55d7f292f062
#include <stdio.h>
int main(int argc, char *argv[])
{
const char *same1 = "same";
const char *same2 = "same";
printf("sameptr:\n");
printf("\tsame1: %p\n", (void*)same1);
printf("\tsame2: %p\n", (void*)same2);
const char *a = "asdf";
const char *b = "basdf";
printf("abptr:\n");
printf("\ta: %p\n", (void*)a);
printf("\tb: %p\n", (void*)b);
const char *digs1 = "1234";
const char *digs2 = "234";
printf("digsptr:\n");
printf("\tdigs1: %p\n", (void*)digs1);
printf("\tdigs2: %p\n", (void*)digs2);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment