char字符串 [英] char string

查看:98
本文介绍了char字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们写的时候


char * p =" xyz" ;


许多书都声明编译器存储xyz在内存中并返回

指向该内存的指针

分配给p。我的问题是,在什么类型的内存中,

堆还是堆栈?


问候

Mangesh。

When we write

char *p = "xyz" ;

Many books state that compiler stores "xyz" in memory and returns
pointer to that memory
which is assigned to p . My question is , in what type of memory ,
heap or stack ?

Regards
Mangesh .

推荐答案

我认为堆栈,因为xyz是一个静态分配的常量。

I think stack, since xyz is a constant statically allocated.




akiross写道:

akiross wrote:
我认为堆栈,因为xyz是一个静态分配的常量。
I think stack, since xyz is a constant statically allocated.




我不喜欢我不知道标准说的是什么,但这不取决于声明的制作地点

?如果它是全局的,那么在程序开始运行之前分配的内存是不是?
?在那种情况下,我不认为
认为它可能在堆栈中。它可能是在初始化

部分。



I don''t know what the standard says, but wouldn''t it depend on where
the statement was made? If it were global, wouldn''t the memory be
allocated before the program started running? In that case, I don''t
think it could be on the stack. It might be in an initialization
section.


2006-04-09,akiross< ak **** ******@gmail.com>写道:
On 2006-04-09, akiross <ak**********@gmail.com> wrote:
我认为堆栈,因为xyz是一个静态分配的常量。
I think stack, since xyz is a constant statically allocated.




他没有指定*其中*他已完成此声明所以你不能

声明它是静态的还是堆的:更具体的说明如果他是

在函数中写这行而不是全局声明

在任何函数定义之外然后这是一回事,里面

函数是另一回事。内部函数,没有静态关键字

那么它是基于堆栈的分配,你无法在

函数返回时可靠地访问它。具有静态的内部功能它可能是堆并且

可以通过调用函数返回指向数据的指针。


标准对此有何看法?



He hasnt specified *where* he has done this declaration so you cant
state whether it is static/heap or not : to be more specific if he is
writing this line in a function as opposed to "global declarations"
outside of any function definitions then this is one thing, inside
functions is another. Inside functions, without the static keyword
then it is a stack based allocation and you cant access it reliably at
function return. Inside functions with "static" it is probably heap and
fine to return a pointer to the data for access by calling functions.

What does the standard say about this?


这篇关于char字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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