On Fri, Nov 28, 2008 at 2:37 PM, Dag Sverre Seljebotn
<[EMAIL PROTECTED]> wrote:
>
>   char* s = "one one one";
>   s[2] = 'Z';
>   printf("%s\n", s);
>
> At least on my gcc, I had two types of behaviour:
>

NO NO NO!!! NEVER DO THAT!!!

In C/C++ string literals should NEVER be modified!!! The actual
contents of the string are placed in special sections of your
executable/library, and such memory places are readonly!!!! So you
hopefully get a segfault, or you just corrupted memory and generated a
extremely hard to find bug!!!.

In short, C string literals should be always bounded to 'const char*'
pointers, and explicit cast to (char*) have to be done with great
care, just because the some  C API  (like older Python ones, and still
some calls in Py3k) do not correctly declare arguments as "const
char*".

Because the danger of modifying the contents of string literals, some
time ago I pushed a fix in cython-devel letting Cython-generated code
to be happy with -Wwrite-strings flag to GCC, explicit casts to
(char*) are done only when extrictly needed.

Regarding compiler optimizations, it seems that GCC is smart enough:


[EMAIL PROTECTED] tmp]$ cat strlits.c
const char *a = "abcabc";
const char *b = "abcabc";
const char *c = "abcabc";

void f(const char* tmp) { }

int main() {
  f("xyzxyz");
  f("xyzxyz");
  f("xyzxyz");
}
[EMAIL PROTECTED] tmp]$ gcc strlits.c
[EMAIL PROTECTED] tmp]$ strings a.out
/lib/ld-linux.so.2
__gmon_start__
libc.so.6
_IO_stdin_used
__libc_start_main
GLIBC_2.0
PTRh
[^_]
abcabc
xyzxyz

As you can see, it seems the literals are only emited once (unless the
strings command do not show dupes, I do not know)

-- 
Lisandro Dalcín
---------------
Centro Internacional de Métodos Computacionales en Ingeniería (CIMEC)
Instituto de Desarrollo Tecnológico para la Industria Química (INTEC)
Consejo Nacional de Investigaciones Científicas y Técnicas (CONICET)
PTLC - Güemes 3450, (3000) Santa Fe, Argentina
Tel/Fax: +54-(0)342-451.1594
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to