Opened 11 months ago

Last modified 11 months ago

#210 new enhancement

Should string literals be const, like in C++?

Reported by: mlbrooks Owned by:
Priority: minor Component: cfa-cc
Version: 1.0 Keywords:
Cc:

Description

With array type:

#ifndef __cforall
#include <stdio.h>
#endif

int main(int argc, char ** argv) {
    char s[] = "hello";
    s[3] = 'p';
    s[4] = '!';
    printf("%s\n", s);
}

gcc actual, g++ actual, cfa actual: compile succeeds, run prints "help!"

With pointer type:

#ifndef __cforall
#include <stdio.h>
#endif

int main(int argc, char ** argv) {
    char *s = "hello";
    s[3] = 'p';
    s[4] = '!';
    printf("%s\n", s);
}

gcc actual, cfa actual: compile succeeds, run gets segmentation fault

g++ actual: compiler warning, ISO C++ forbids converting a string constant to 'char*'

Change History (1)

comment:1 Changed 11 months ago by ajbeach

I vote for this. The only downside is C->CFA code might require adding const in a few more places but only in places where it helps anyways.

Note: See TracTickets for help on using tickets.