使用字符串文字进行数组初始化的内存 [英] memory for array initialization with string literal

查看:61
本文介绍了使用字符串文字进行数组初始化的内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在经历 QA 表示使用字符串文字初始化 char 数组将导致两种内存分配,一种用于变量,另一种用于字符串文字.

I was going through this QA where it is said that char array when initialized with string literal will cause two memory allocations one for variable and other for string literal.

我在下面编写了程序,以了解如何分配内存.

I have written below program to see how is the memory allocated.

#include <stdio.h>
#include <string.h>

int main()
{
    char a[] = "123454321";
    
    printf("a =%p and &a = %p\n", a, &a);

    for(int i = 0; i< strlen(a); i++)
        printf("&a[%d] =%p and a[%d] = %c\n",i,&a[i],i,a[i]);
    
    return 0;
}

,输出为:

a =0x7ffdae87858e and &a = 0x7ffdae87858e                                                                             
&a[0] =0x7ffdae87858e and a[0] = 1                                                                                    
&a[1] =0x7ffdae87858f and a[1] = 2                                                                                    
&a[2] =0x7ffdae878590 and a[2] = 3                                                                                    
&a[3] =0x7ffdae878591 and a[3] = 4                                                                                    
&a[4] =0x7ffdae878592 and a[4] = 5                                                                                    
&a[5] =0x7ffdae878593 and a[5] = 4                                                                                    
&a[6] =0x7ffdae878594 and a[6] = 3                                                                                    
&a[7] =0x7ffdae878595 and a[7] = 2                                                                                    
&a[8] =0x7ffdae878596 and a[8] = 1

从输出中看,我们似乎没有两个分别用于数组和字符串文字的内存位置.

From the output it does not look like we have two separate memory locations for array and string literal.

如果我们有分别用于数组和字符串文字的内存,在这种情况下,有什么方法可以证明数组 a 和字符串文字分别存储吗?

If we have separate memory for array and string literal, is there any way we can prove array a and string literal stores separately in this scenario?

克隆链接: https://onlinegdb.com/HkJhdSHyd

推荐答案

char a [] ="123454321";

从技术上讲,字符串文字"123454321" 不需要存储在任何地方.只需输入 main 时,使用正确的值初始化 a [] .无论是通过从某个静态只读内存位置复制字符串来完成此操作,还是运行以其他方式填充该字符串的代码,都是标准规定的.

Technically, the string literal "123454321" is not required to be stored anywhere as such. All that's required is that a[] be initialized with the right values when main is entered. Whether that's done by copying the string from some static read-only memory location, or running code that fills it in some other way is not mandated by the standard.

就标准而言,为了初始化 a [] ,编译器发出与以下代码等效的代码是完全可以接受的:

As far as the standard goes, it would be perfectly acceptable for the compiler to emit code equivalent to the following in order to initialize a[]:

char a[10];
for(int n = 0; n <= 4; n++)
    a[n] = a[8-n] = '1' + n;
a[9] = '\0';

实际上,至少有一个编译器( gcc )初始化 a [] 通过自定义代码,而不是存储和复制文字字符串.

In fact, at least one compiler (gcc) initializes a[] via custom code, rather than storing and copying the literal string.

mov     DWORD PTR [ebp-22], 875770417    ; =  0x34333231  =  '1', '2', '3', '4'
mov     DWORD PTR [ebp-18], 842216501    ; =  0x32333435  =  '5`, '4', '3', '2'
mov     WORD  PTR [ebp-14], 49           ; =  0x31        =  '1', '\0'

这篇关于使用字符串文字进行数组初始化的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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