什么时候c ++分配/取消分配字符串字面量 [英] when does c++ allocate/deallocate string literals

查看:162
本文介绍了什么时候c ++分配/取消分配字符串字面量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此示例中,在程序的生命周期中,何时分配和释放字符串字面值hello?

When is the string literal "hello" allocated and deallocated during the lifetime of the program in this example?

init(char **s)
{ 
  *s = "hello";
}
int f()
{
  char *s = 0;
  init(&s);
  printf("%s\n", s);
  return 0;
}


推荐答案

字符串文字被初始化为只读存储器段由编译器。在运行时没有初始化或删除。

The string literal is initialised into read-only memory segment by the compiler. There is no initialisation or removal done at run-time.

这篇关于什么时候c ++分配/取消分配字符串字面量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆