Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to return a pointer to a string literal to the caller?

char* stringReturn()
{
char a[] = "Array of characters";
//return a; // I know stack allocation should not be returned

char *b = "Pointer to a string";
return b; // Is it safe ? 
} 


int main() {    
    char *str = stringReturn ();
    cout<< str; 
    return 0; }

This is safe means then where the data "Pointer to a string" will be stored in the memory.

like image 785
SridharKritha Avatar asked Apr 02 '12 17:04

SridharKritha


People also ask

Can you return a string literal?

Strings in C are arrays of char elements, so we can't really return a string - we must return a pointer to the first element of the string. All forms are perfectly valid. Note the use of const , because from the function I'm returning a string literal, a string defined in double quotes, which is a constant.

Can you return a string literal in C?

It's perfectly valid to return a pointer to a string literal from a function, as a string literal exists throughout the entire execution of the program, just as a static or a global variable would.

Can you have pointer to a string?

A pointer to a string in C can be used to point to the base address of the string array, and its value can be dereferenced to get the value of the string. To get the value of the string array is iterated using a while loop until a null character is encountered.

Can a char pointer hold a string?

In C, a string is an array of characters that end in a null (0). So a char * doesn't contain a string, but it can be the address of or pointer to a string.


2 Answers

Yes, it is safe to return the value of b. The string literal to which b points has static storage duration.

But, you must declare your pointers const-correctly. b must be const char*b.

like image 118
Robᵩ Avatar answered Oct 19 '22 18:10

Robᵩ


There is a difference between the char[] and char* variable declarations. As you noted in the comment in the code, char a[] allocates the memory for a on the stack and copyies the characters to that array, essentially making the declaration the same as:

char a[] = {'A', 'r', 'r', 'a', 'y', ' ', 'o', 'f', ..., '\0'};

When the function exits, the memory allocated on the stack for the array is gone, no longer making it safe to refer to via pointers.

When delcaring the variable via char* b, the address of a statically allocated char array is stored in the memory for pointer b. The actual memory where the string is allocated is not specified by the standard, but will be accessible throughout the execution of the code (in fact the compiler might reuse that memory if you declare another variable of char* with the exact same string) -- so it is safe to pass that address around (e.g as a return value from a function).

As Rob pointed out, you should declare the type of the pointer const char* to avoid accidentally trying to write to the memory where the string is stored: it is not guaranteed by the standard to have been placed in a writable segment of the memory. If you need to change it, you need to dynamically allocate the memory, copy the string into that allocated memory and return/work with that.

like image 27
Attila Avatar answered Oct 19 '22 18:10

Attila