你是在谈论C字符串,这实际上是焦炭的载体。在C++中,使用std::string
类,并且创建一个常量字符串为const std::string
。
无论如何,编译器,以便存储字面字符串在源代码显示保留将来节目一块内存。这部分内存被认为是只读的,所以你应该用const char *
指向它。它的大小正好是字符串的大小加上尾部零的一个额外位置,标记字符串的结尾。
编译器需要保持向后兼容性,所以他们还是接受文字由char *
指出。但是,这是误导性的,因为您不应该修改可以存储在嵌入式系统ROM中的内存。
在我的系统,我使用铛:
$ clang --version
Ubuntu clang version 3.0-6ubuntu3 (tags/RELEASE_30/final) (based on LLVM 3.0)
Target: i386-pc-linux-gnu
Thread model: posix
在铛C编译器,这个代码编译没有错误:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char * str = "Hello, World!";
printf("%s", str);
return EXIT_SUCCESS;
}
然而,同样的代码(稍作修改,例如作为标题的名称)编译为C++程序时会引发以下警告:
kk.cpp:6:15: warning: conversion from string literal to 'char *' is deprecated [-Wdeprecated-writable-strings]
char * str = "Hello, World!";
^
1 warning generated.
希望能够帮助。
只是可以肯定(我看到C++代码)C或C++程序? – PiotrNycz
我相信这将只能扔警告如果有的话,如果指定的char * =为const char * ... – anishsane
可以尝试写最少的代码,编译成.O&采取objdump的的.o文件将的;只是为了看看这个fpp驻留在RO内存中还是RAM中。 – anishsane