user8235767
user8235767

Reputation:

function works ok w/o "const" flag. importance of "const"?

The function below uses const char *s1

What the function does is perhaps not important. It returns 1 if the string contains characters other than allowed characters. It returns 0 if it doesn't.

int TEST (const char *s1);

int TEST (const char *s1) {
    char * s2= "o123";
    return s1[ strspn(s1, s2) ] != '\0';
} 

The function seems to work just fine if I were to remove const portion from it.

Why do I need const terminology in that function, if it seems to work fine without it? What is the importance of it? What are the implications of

Upvotes: 1

Views: 178

Answers (4)

Andre Kampling
Andre Kampling

Reputation: 5631

The usage of const prevents you or somebody else working with your code from doing something accidentally wrong. Moreover it is for documentation purposes as the reader of the code knows that these functions are not changing the characters (read-only) the pointer points to.

Further the compiler may optimize your code if you use const correctness because it knows that these values are not changing inside the functions being read-only. But the first reason of documenting and making your function safer is more important.

There are three forms you always come accross with: char* s1, const char* s1 and const char* const s1. The forth but rarely needed form is: char* const. The meanings are as follows:

1. char* s1:

In this case s1 is just a pointer to memory of one or more characters. The following can be/can not be done inside of the function:

/* Function might changes content of s1 */
int TEST (char* s1)
{
   s1[0] = 'A'; /* works */
   *s1 = 'A';   /* same as above works */
   ++s1;        /* this works because the copy of the pointer is non constant */
   ...
}

The following calls can be/can not be made:

char*       str;
const char* const_str;
...
TEST(str);       /* works as str as no restrictions */
TEST(const_str); /* fails because function might change const const_str */

2. const char* s1:

The term: const char* s1 means s1 is a pointer to memory of one or more characters that can't be changed with this pointer. The following can be/can not be done inside of the function:

/* Function is read-only and doesn't changes content of s1 */
int CONST_TEST (const char* s1)
{
   s1[0] = 'A'; /* compiler fail */
   *s1 = 'A';   /* same as above compiler fail */
   ++s1;        /* this works because the copy of the pointer is non constant */
   ...
}

The following calls can be/can not be made:

char*       str;
const char* const_str;
...
CONST_TEST(str);       /* works as str as no restrictions */
CONST_TEST(const_str); /* works because function is read-only and const_str is it also */

The compiler will fail compiling and tell you that you try to write to a memory location that is marked as constant.


3. const char* const s1:

This means: s1 is a constant pointer to memory of one or more characters that can't be changed with this pointer. This is an extension to the first approach, the pointer s1 itself is passed by value and this copy can't be changes inside the function. So that you can not do something like ++s1 inside the function, therefore the pointer will allways point to the same memory location.


4. char* const s1:

This is like case 3 but without s1 pointing to memory marked as constant. Therefore the content s1 is pointing to can be changed like case 1 but with a pointer being constant.

Upvotes: 1

chux
chux

Reputation: 153348

importance of “const”?

3 reasons:

Applicability
Code correctness
Speed


Consider what the calling code is allowed to do

int TEST_with_const(const char *s1);
int TEST_without_const(char *s1);

char *cs;
const char *ccs
...
TEST_with_const(cs); // allowed
TEST_with_const(ccs); // allowed
TEST_without_const(cs); // allowed
TEST_without_const(ccs); // fails to compile.

The last one fails because TEST_without_const(char *s1); is telling the outside world, I want a pointer to memory that I might alter. ccs is not a pointer to modifiable memory.

Assuming TEST_without_const(char *s1); has the same body of int TEST (const char *s1), it did not need a pointer to modifiable memory


By using const char *s1 the function's body will complain during compilation should a modification of the memory pointed to by s1 is attempted. If the function design does not need to modify the memory pointed to by s1, this check helps insure correctness.


Depending on complier, some optimizations can occur with const char *, but not with char *.

Upvotes: 2

Some programmer dude
Some programmer dude

Reputation: 409166

When you have const char *s1 you are telling the compiler that you will not modify the contents of the memory that s1 is pointing to. It's a semantic signal for the compiler so you do not make mistakes in the function (attempts to change the contents will results in errors). It might also allow the compiler to add some optimizations.

But more importantly it's a signal to other programmers reading your code, and using your function. And with "other programmers" I also include you in a few weeks, months or years time, when you might come back to the code and have forgotten the details of it.

Upvotes: 4

unalignedmemoryaccess
unalignedmemoryaccess

Reputation: 7441

const in this function prevents you to write something to memory where variable points to.

Here, you are only checking value if it is not equal to \0 and it is ok because you do only read operation.

int TEST (const char *s1) {
    char * s2= "o123";
    return s1[ strspn(s1, s2) ] != '\0';
} 

If you do this, trying to write through s1 pointer:

int TEST (const char *s1) {
    char * s2= "o123";
    s1[0] = '4'; //Error, s1 is const pointer, can't write to memory pointed to by s1
    return s1[ strspn(s1, s2) ] != '\0';
} 

const pointer in your case means that this function should not write any data to input pointer, only read operation can be performed. You can in fact change where s1 points, only write to memory through s1 is not allowed. This is to prevent any mistakes to now write unexpectedly because it may in some cases lead to undefined behaviour.

Upvotes: 1

Related Questions