Which of the below is the best practice when working with C strings?
char full_name[] = "foo boo";
char * full_name = "foo boo";
What are the pros and cons for each of them from a security standpoint?
Which of the below is the best practice when working with C strings?
char full_name[] = "foo boo";
char * full_name = "foo boo";
What are the pros and cons for each of them from a security standpoint?
Working with c strings is a complex task, none of the options you show has any advantages or disadvantages. It really depends on the solution you want to implement.
char full_name[] = "foo boo";
is an array with the following contents
{'f', 'o', 'o', ' ', 'b', 'o', 'o', '\0'}
you can modify it but it cannot grow, and
char *full_name = "foo boo";
is a string literal, you cannot modify nor can it grow and it's better to define it like this
const char *full_name = "foo boo";
even though this does not completely prevents modifying it, it helps not doing it accidentally.
Modifying a string literal is possible, it's just wrong because it invokes undefined behavior.
In C
, a string can be referred either using a character pointer
or as a character array
.
Strings as character arrays
char str[4] = "GfG"; /*One extra for string terminator*/
/* OR */
char str[4] = {‘G’, ‘f’, ‘G’, '\0'}; /* '\0' is string terminator */
When strings are declared as character arrays, they are stored like other types of arrays in C. For example, if str[]
is an auto variable then string is stored in stack segment
, if it’s a global
or static
variable then stored in data segment, etc.
Strings using character pointers
Using character pointer
strings can be stored in two ways:
Read-only string in a shared segment.
When string value is directly assigned to a pointer, in most of the compilers, it’s stored in a read only block (generally in data segment
) that is shared among functions.
char *str = "GfG";
In the above line “GfG”
is stored in a shared read-only location, but pointer str
is stored in a read-write memory. You can change str
to point something else but cannot change value at present str
. So this kind of string should only be used when we don’t want to modify string at a later stage in program.
Dynamically allocated in heap segment
.
Strings are stored like other dynamically allocated things in C
and can be shared among functions.
char *str;
int size = 4; /*one extra for ‘\0’*/
str = malloc(size);
*(str+0) = 'G';
*(str+1) = 'f';
*(str+2) = 'G';
*(str+3) = '\0';
If you see the security point of view then don't use pointers. Try to use always non pointer variables. Pointer access the memory address directly which may leads to memory leak issues, memory hack issues etc.