在哪里为内存分配一个const char指针? [英] Where is memory allocated with a const char pointer?

查看:101
本文介绍了在哪里为内存分配一个const char指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:



C ++字符串文字数据类型存储



$ b b

在此代码中:

In this code:

const char * str =hello world;

如果我理解正确,一个指针是4或8个字节,我猜测会在堆栈上分配。
但是hello world的内存分配和存储在哪里?

或者 str 指向什么?

If I understand correctly a pointer is 4 or 8 bytes, which I guess would be allocated on the stack. But where is the memory for the "hello world" allocated and stored?
Or what does str point to exactly?

推荐答案

未分配。它通常存储在程序的代码段或堆栈由编译器。无论哪种方式,它指向一个以null结尾的字符数组。

It's not allocated. It's generally stored in your program's code segment or on the stack That's up to the compiler. Either way, it points to a null-terminated array of characters.

这篇关于在哪里为内存分配一个const char指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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