请有人解释为什么以下C程序崩溃:
void changeChar(char *string);
int main(int argc, char *argv[])
{
char *test = "word";
changeChar(test);
return 0;
}
void changeChar(char *string) {
*string = 'A';
}
Run Code Online (Sandbox Code Playgroud)
而以下代码完美地运作:
void changeChar(char *string);
int main(int argc, char *argv[])
{
char test[] = "word";
changeChar(test);
return 0;
}
void changeChar(char *string) {
*string = 'A';
}
Run Code Online (Sandbox Code Playgroud) #include<stdio.h>
int main ()
{
char *s="FIGHT" ;
printf("\n Whole string is %s ", s ); // Printing FIGHT -- this is fine
s[0]='L' ;
printf ("\n Now whole string is %s", s ); // Printing LIGHT -- My Question is how string literal constant is getting modified when it is being stored in read only memory .
}
Run Code Online (Sandbox Code Playgroud)
以上代码在我的系统上工作正常.
在gcc上调试时,我发现Unicode文字u"????"表示为u"\007\116\015\116\227\137\362\135".哪个有意义 - ?是0x4E07,而八进制中的0x4E是116.
现在在基于Intel的Macbook上的Apple LLVM 9.1.0上,我发现相同的文字不会被处理为相同的字符串,即:
u16string{u"????"} == u16string{u"\007\116\015\116\227\137\362\135"}
Run Code Online (Sandbox Code Playgroud)
从去true到false.我还在使用小端系统,所以我不明白发生了什么.
NB.我不是想使用对应关系u"????"== u"\007\116\015\116\227\137\362\135".我只是想了解发生了什么.
如何释放a char*后使用的所有内存不再有用?
我有一些 struct
struct information
{
/* code */
char * fileName;
}
Run Code Online (Sandbox Code Playgroud)
我显然要在其中保存一个文件名char*,但在使用一段时间之后,我想释放以前的内存,我该怎么做?
E:我并不是要释放指针,而是指向的空间fileName,这很可能是一个字符串文字.
字符串文字是左值,它打开门以修改字符串文字.
来自C in a Nutshell:
在C源代码中,文字是表示固定值的标记,可以是整数,浮点数,字符或字符串.文字的类型由其值和符号决定.这里讨论的文字不同于C99标准中引入的复合文字.复合文字是普通的可修改对象,类似于变量.
虽然 C不严格禁止修改字符串文字,但您不应尝试这样做.首先,编译器将字符串文字视为常量,可将其置于只读存储器中,在这种情况下,尝试的写操作会导致错误.另一方面,如果程序中使用了两个或多个相同的字符串文字,编译器可能会将它们存储在同一位置,因此当您访问另一个时,修改它会导致意外结果.
第一段说"C中的字面值表示固定值".
这是否意味着不应修改文字(复合文字除外)?
由于字符串文字不是复合文字,是否应修改字符串文字?
这两段是否相互矛盾?我怎么理解他们?
可以修改既不是复合文字也不是字符串文字的文字?
这是一个使用带有大量子元素的三引号f-string的函数:
def pass_empty_string(param):
from lxml import etree
xml = etree.XML(f'''
<root>
<child>text</child>
<child>{param}</child>
...
</root>''')
return xml
Run Code Online (Sandbox Code Playgroud)
获得或重视</child>时param是否可以获得空元素?None''
假设我有一个函数,它接受一个字符串作为输入:
SomeOutputType f_impl(const char* s);
Run Code Online (Sandbox Code Playgroud)
大多数呼叫站点只使用字符串文字作为输入,例如f("Hello, world").假设我已经实现了以下函数来在编译时计算结果
template <char...> SomeOutputType f_impl();
Run Code Online (Sandbox Code Playgroud)
我的问题是,有没有办法让调用网站像f("Hello, world")调用模板形式,而对于一般调用网站,如string s="Hello, world"; f(s.c_str());调用一般形式?为了澄清,auto s = "Hello, world"; f(s);不必调用模板化形式,因为s它现在是一个变量而不再是编译时常量.
这个问题的一个有用的例子是优化printf.在大多数情况下,format将是字符串文字,因此可以在编译时完成很多事情来优化事物,而不是format在运行时解析.
/**** Program to find the sizeof string literal ****/
#include<stdio.h>
int main(void)
{
printf("%d\n",sizeof("a"));
/***The string literal here consist of a character and null character,
so in memory the ascii values of both the characters (0 and 97) will be
stored respectively which are found to be in integer datatype each
occupying 4 bytes. why is the compiler returning me 2 bytes instead of 8 bytes?***/
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
2
Run Code Online (Sandbox Code Playgroud) c sizeof type-conversion string-literals implicit-conversion