Please consider the following C++ code fragment. I am passing a reference to a char * as a parameter to a function.
void name(char **str){
    *str=(char *)"JUSTFORFUN";
}
int main(int argc, const char * argv[])
{
    char *test;
    name(&test);
    cout<<test; //Prints "JUSTFORFUN"
    //delete(test); //Throws error "pointer being freed was not allocated"
    return 0;
}
I think the memory allocated for storing "JUSTFORFUN" in function name() is allocated on a stack. So when the control goes out of name(), the memory associated with (char *)"JUSTFORFUN" should have been freed by the compiler. My question is why do I still get the correct output when I print test? Shouldn't it have printed a junk value?
When I do something similar for int. I get the expected result.
void nameint(int **value){
    int val=5;
    *value=&val;
}
int main(int argc, const char * argv[])
{
    int *val;
    nameint(&val);
    cout<<*val; //Prints a junk value 1073828160    
    return 0;
}
Why is there a difference between behavior of int and char * ?
 
     
     
    