char * ptr =" hello"和char carray [] =" hello" [英] char * ptr = "hello" and char carray[] = "hello"

查看:63
本文介绍了char * ptr =" hello"和char carray [] =" hello"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下示例代码


char * ptr =" hello";

char carray [] =" hello";

int main(无效){

}


标准对存储要求有什么说法

关于ptr和carray?是否公平声明char * ptr将在DATA段中花费4个多字节(在32位平台上)
?我发现

语句至少与gcc 2.96一致。我假设在某些

条件下编译器可以优化存储?


感谢您的评论,


Fei

Consider the following sample code

char * ptr = "hello";
char carray[] = "hello";
int main(void){
}

What does the standard have to say about the storage requirement
about ptr and carray? Is it a fair statement that char *ptr will take
4 more bytes (on 32bit platform) in DATA segment? I have found
the statement true at least with gcc 2.96. I assume under certain
condition the compiler can optimize the storage away?

Thanks for your comments,

Fei

推荐答案

" fe ***** @ gmail.com" < FE ***** @ gmail.com>写道:
"fe*****@gmail.com" <fe*****@gmail.com> writes:
考虑以下示例代码

char * ptr =" hello";
char carray [] =" hello";
int main(void){
}

有关ptr和carray的存储要求的标准是什么? char * ptr在DATA段中将需要多4个字节(在32位平台上)这是一个公平的陈述吗?我发现
声明至少与gcc 2.96一致。我假设在某些条件下编译器可以优化存储?
Consider the following sample code

char * ptr = "hello";
char carray[] = "hello";
int main(void){
}

What does the standard have to say about the storage requirement
about ptr and carray? Is it a fair statement that char *ptr will take
4 more bytes (on 32bit platform) in DATA segment? I have found
the statement true at least with gcc 2.96. I assume under certain
condition the compiler can optimize the storage away?




任何不影响程序输出的优化都是
允许
。这包括消除未使用的对象。


除非这样,ptr将占用sizeof(char *)字节,第一个字符串

literal" hello"将占用6个字节,而carray将占用另外6个b
字节。所有这些都将具有静态存储持续时间,这意味着它们在程序的生命周期中存在。 (C没有定义

任何称为DATA段的东西。)


-

Keith Thompson( The_Other_Keith) ks***@mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



Any optimization that doens''t affect the output of the program is
permitted. This includes eliminating unused objects.

Barring that, ptr will occupy sizeof(char*) bytes, the first string
literal "hello" will occupy 6 bytes, and carray will occupy another 6
bytes. All of these will have static storage duration, meaning that
they exist for the lifetime of the program. (C doesn''t define
anything called a "DATA segment".)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


fe ***** @ gmail。 com 写道:
fe*****@gmail.com wrote:
请考虑以下示例代码

char * ptr =" hello";
char carray [] = 你好;
int main(无效){
}

关于ptr和carray的存储要求,标准有什么说法?是否公平声明char * ptr将在DATA段中多花费4个字节(在32位平台上)?
Consider the following sample code

char * ptr = "hello";
char carray[] = "hello";
int main(void){
}

What does the standard have to say about the storage requirement
about ptr and carray? Is it a fair statement that char *ptr will take
4 more bytes (on 32bit platform) in DATA segment?




编号从概念上讲,不是。 br />

从概念上讲,第一个声明会创建一个[不可修改的]字符串

literal" hello"在静态内存中(占用6个字节)和指向该字符串文字的指针(例如,

4个字节)。这总共是10个字节。


第二个声明在静态内存中创建一个[不可修改的]字符串文字hello

并且还分配一个可修改的6个字符的数组,将通过从程序

startup中的字符串文字复制数据来初始化

。这需要总共12个字节。


这意味着第二次delaration需要更多的内存,而不是
。但这只是一个纯粹的概念观点。


在实践中,允许编译器合并相同的字符串文字

并执行其他类型的优化,这可能是显着

会影响这种情况下的内存消耗。


-

祝你好运,

Andrey Tarasevich



No. Conceptually, no.

Conceptually, the first declaration creates a [non-modifyable] string
literal "hello" in static memory (occupying 6 bytes) and a pointer (say,
4 bytes) pointing to that string literal. This is 10 bytes total.

The second declaration creates a [non-modifyable] string literal "hello"
in static memory and also allocates a modifyable array of 6 chars, which
will be initialized by copying data from the string literal at program
startup. This requores 12 bytes total.

This means that the second delaration requires more memory that the
first. But that is a purely conceptual point of view.

In practice the compiler is allowed to merge identical string literals
and perform other types of optimizations, which might significantly
affect the memory consumption in cases like this.

--
Best regards,
Andrey Tarasevich


Andrey Tarasevich写道:
Andrey Tarasevich wrote:

fe ***** @ gmail.com 写道:

fe*****@gmail.com wrote:
请考虑以下示例代码

char * ptr =" hello" ;;
char carray [] =" hello";
int main(void){
}

标准对存储有什么看法要求
关于ptr和carray? char * ptr在DATA段中将需要多4个字节(在32位平台上)这是一个公平的陈述吗?
Consider the following sample code

char * ptr = "hello";
char carray[] = "hello";
int main(void){
}

What does the standard have to say about the storage requirement
about ptr and carray? Is it a fair statement that char *ptr will take
4 more bytes (on 32bit platform) in DATA segment?



在概念上,没有。
<从概念上讲,第一个声明创建了一个[不可修改的]字符串
literal" hello"在静态内存中(占用6个字节)和指向该字符串文字的指针(例如,
4个字节)。总共10个字节。

第二个声明在静态内存中创建一个
[不可修改的]字符串文字hello
并分配
一个可修改的6个字符的数组,将通过在程序启动时从字符串文字复制数据来初始化。这需要总共12个字节。

这意味着第二次delaration需要更多的内存,首先是
。但这是一个纯粹的概念观点。



No. Conceptually, no.

Conceptually, the first declaration creates a [non-modifyable] string
literal "hello" in static memory (occupying 6 bytes) and a pointer (say,
4 bytes) pointing to that string literal. This is 10 bytes total.

The second declaration creates a
[non-modifyable] string literal "hello"
in static memory and also allocates
a modifyable array of 6 chars, which
will be initialized by copying data from the string literal at program
startup. This requores 12 bytes total.

This means that the second delaration requires more memory that the
first. But that is a purely conceptual point of view.




我不同意第二种语义。

char carray [] ="你好&;;

是简写为

char carray [] = {''h'',''e'','l'',''l' ','''',''\ n''};

这意味着数组的初始化程序

可以嵌入到操作码中,而

初始化程序不需要与其他字符串文字存在于同一种类型的内存中。


-

pete



I disagree about the semantics of the second.
char carray[] = "hello";
is shorthand for
char carray[] = {''h'',''e'',''l'',''l'',''o'',''\n''};
which means that the initialiser for the array
can be embedded in the opcode and that
the initialiser need not exist in the same kind of
memory as other string literals.

--
pete


这篇关于char * ptr =&quot; hello&quot;和char carray [] =&quot; hello&quot;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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