Optimization with GCC

1

Recently I've been wondering if it's possible for the compiler with some flag optimization, avoid copying two arrays to the .rodata section? Thus, memory addresses would be the same, eg

const char str[7] = "string";

const char str1[7] = "string";


int printf(const char *format, ...);

int main(void) {

     if(str == str1) 
         printf("Endereços de memória iguais");

     return 0;

}

So in this example above, is it possible that somehow the compiler uses the same memory addresses?

EDIT:

It does not make much sense at all, of course, it was just a matter of curiosity, I tried to see some flags of optimization but no success.

#include <stdio.h>

void main(void) {

     char *str1 = "string";
     char *str2 = "string";

     if(str == str2) puts("Igual");
}

In the example above, the compiler puts the string literal of the pointer str1 in the .rodata section and uses the same string , ie the same address to the str2 pointer. So I wanted to know if you could simulate this by doing array . I read the Manpage of the GCC, but I did not find anything relevant in this sense, but thank you anyway for your response.

    
asked by anonymous 31.10.2018 / 03:13

1 answer

1

Yes, it is possible, although this code does not seem to make much sense:

#include <stdio.h>

int main(void) {
    const char str[7] = "string";
    const char *str1 = str;
    if(str == str1) printf("Endereços de memória iguais");
}

See running ideone . And no Coding Ground . Also put it in GitHub for future reference .

I know I expected another answer, but another does not make sense. In C if you want an optimization, optimize, do not expect the compiler to do it for you.

    
31.10.2018 / 04:18