free()ing a _strdup'd value correctly?

In this EXAMPLE, am I free()ing this _strdup()'d return value correctly?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
char *getText();

void someFunction(void)
{
    char *msg = NULL;
    msg = &*getText());
    
    // This prints out "Hello World"
    printf("%s", msg);
    
    if (msg)
        free (msg);
}

char *getText()
{
    std::string retVal = "Hello World"; 
    return (_strdup(retVal.c_str()));
}


Thanks!
Yes. You don't actually need line 11. Also, lines 5 and 6 should look like

char *msg = getText();

Hope this helps.
It does help, and I thank you.
Sorry, it's late and my brain is not functioning. I meant to also say that line 11 would be a good idea before line 9. Don't printf( "%s", NULL );

Enjoy!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
char *getText();

void someFunction(void)
{
    char *msg = getText();
    if (msg)
    {
        printf("%s", msg);
        free (msg);
    }
}

char *getText()
{
    std::string retVal = "Hello World"; 
    return (_strdup(retVal.c_str()));
}


Love it. Thanks!
Topic archived. No new replies allowed.