指针和存储 [英] pointer and storage

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

问题描述

嗨。我对以下声明有疑问。


char * a =" hello";


问题是在哪里你好得到存储。它在一些静态区域

,堆栈或堆。我观察到试图修改你好

会导致分段错误。谢谢你的任何帮助。


Eric

Hi.I have a question on the following statement.

char* a="hello";

The question is where "hello" gets stored.Is it in some static area
,stack or heap.I have observed that attempting to modify "hello"
results in segmentation fault.Thanks for any help.

Eric

推荐答案


di ********** @ yahoo.com 写道:

您好。我对以下声明有疑问。


char * a =" hello";


问题是你好的地方得到存储。它在一些静态区域

,堆栈或堆。我观察到试图修改你好

会导致分段错误。谢谢你的任何帮助。
Hi.I have a question on the following statement.

char* a="hello";

The question is where "hello" gets stored.Is it in some static area
,stack or heap.I have observed that attempting to modify "hello"
results in segmentation fault.Thanks for any help.



* a将存储在数据区的只读部分。

这就是分段错误背后的原因。

-Deepak。

The *a will be stored in the readonly part of the data area.
Thats the reason behind the segmenatation fault.

-Deepak.


>

Eric
>
Eric


di ********** @ yahoo.com 说:

您好。我对以下声明有疑问。


char * a =" hello";


问题是你好在哪里?得到存储。它在一些静态区域

,堆栈或堆。
Hi.I have a question on the following statement.

char* a="hello";

The question is where "hello" gets stored.Is it in some static area
,stack or heap.



这取决于实施。使用

堆栈或堆不需要实现。


建议您使用const char *而不仅仅是char *。

It depends on the implementation. No implementation is required to use
stacks or heaps.

You would be well-advised to use const char * rather than mere char *.


我观察到试图修改hello

会导致分段错误。
I have observed that attempting to modify "hello"
results in segmentation fault.



修改常量字符串导致的行为未定义。

分段错误是一种可能的结果。没有分段

错误是另一种可能的结果。火灾对罗马的破坏是另一种可能的结果。

-

Richard Heathfield

" Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)

The behaviour resulting from modifying a constant string is undefined. A
segmentation fault is one possible result. The absence of a segmentation
fault is another possible result. And the destruction of Rome by fire is
another possible result.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


di ********** @ yahoo.com 发布:
di**********@yahoo.com posted:

char * a =" hello" ;;
char* a="hello";



这是指向非const char的非const指针的定义。它也是
初始化指向字符串文字地址的指针(建议使用
。)


This is a definition of a non-const pointer to a non-const char. It also
initialises the pointer to the address of a string literal (which is il-
advised.)


问题是在哪里你好得到存储。它在一些静态区域

,堆栈或堆。我观察到试图修改你好

会导致分段错误。谢谢你的任何帮助。
The question is where "hello" gets stored.Is it in some static area
,stack or heap.I have observed that attempting to modify "hello"
results in segmentation fault.Thanks for any help.



以下两个程序是等价的:


/ *程序1 * /


int main(无效)

{

char const * p =" Hello" ;;返回0;

}


/ *程序2 * /


char const str_literal1 [] = {' 'H'','e'','l'','l'',''o'',0};


#define LITERAL1(*( char(*)[sizeof str_literal1])& str_literal1)


int main(void)

{

char const * p = LITERAL1;返回0;

}


-


Frederick Gotham


The following two programs are equivalent:

/* Program 1 */

int main(void)
{
char const *p = "Hello"; return 0;
}

/* Program 2 */

char const str_literal1[] = {''H'',''e'',''l'',''l'',''o'',0};

#define LITERAL1 (*(char(*)[sizeof str_literal1])&str_literal1)

int main(void)
{
char const *p = LITERAL1; return 0;
}

--

Frederick Gotham


这篇关于指针和存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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