I have o2 compiler flag set. Tested on multiple platforms/compilers. Both variables are not used at all.
const char * temp1 = "a";// optimised away
char * temp2 = new char[1];//not optimised.
First variable is dropped away by compiler since it is redundant. The second variable counterwise. Is compiler unable to detect that this memory chunk is used nowhere in the program?
The original issue comes from real product when I noticed that temporary, unused std::string longer than 15 signs and passed as function's parameter is not optimized away.
asm view online: https://godbolt.org/g/Shmx92
Edit: as Jarod42 mentioned clang optimises both variables away.