简体   繁体   English

字符数组 VS 字符 *

[英]Char Array VS Char *

This is a question based on answers from question:这是一个基于问题答案的问题:

const char myVar* vs. const char myVar[] const char myVar* 与 const char myVar[]

const char* x = "Hello World!";
const char  x[] = "Hello World!";

I understand the difference now, but my new questions are:我现在了解其中的区别,但我的新问题是:

(1) What happens to the "Hello World" string in the first line if I reassign x? (1) 如果我重新分配 x,第一行中的“Hello World”字符串会发生什么情况? Nothing will be pointing to it by that point - would it be destroyed when the scope ended?到那时,什么都不会指向它 - 当 scope 结束时它会被破坏吗?

(2) Aside from the const-ness, how are the values in the two examples differently stored in memory by the compiler? (2) 除了常量之外,编译器在 memory 中存储的两个示例中的值有何不同?

Placing "Hello World!"放置"Hello World!" in your code causes the compiler to include that string in the compiled executable.在您的代码中导致编译器将该字符串包含在已编译的可执行文件中。 When the program is executed that string is created in memory before the call to main and, I believe, even before the assembly call to __start (which is when static initializers begin running).当程序执行时,在调用main之前在 memory 中创建字符串,我相信,甚至在调用__start的程序集之前(这是 static 初始化程序开始运行的时间)。 The contents of char * x are not allocated using new or malloc , or in the stack frame of main , and therefore cannot be unallocated. char * x的内容不是使用newmalloc分配的,也不是在main的堆栈帧中,因此不能取消分配。

However, a char x[20] = "Hello World" declared within a function or method is allocated on the stack, and while in scope, there will actually be two copies of that "Hello World" in memory - one pre-loaded with the executable, one in the stack-allocated buffer.然而,在 function 或方法中声明的char x[20] = "Hello World"被分配在堆栈上,而在 scope 中,实际上将在 ZCD69B4957F06CD8191ZBEF3 中存在该"Hello World"的两个副本可执行文件,位于堆栈分配的缓冲区中。

The compiler stores the first in a section of memory called RODATA (read-only data).编译器将第一个存储在 memory 中称为 RODATA(只读数据)的部分中。 As long as the program is still running, the memory still holds its initial value.只要程序仍在运行,memory 仍保持其初始值。

The second is stored just like any other array--on the stack.第二个像任何其他数组一样存储在堆栈上。 Just like any other local variable, it could be overwritten once its scope ends.就像任何其他局部变量一样,一旦它的 scope 结束,它就可以被覆盖。

  1. Formally in both cases the "Hello World!"在这两种情况下都是正式的"Hello World!" string is allocated in static memory as a contiguous sequence of char , so there is no dynamically allocated memory to reclaim nor any class instance to destroy; string is allocated in static memory as a contiguous sequence of char , so there is no dynamically allocated memory to reclaim nor any class instance to destroy;
  2. Both x s will be allocated either in static memory or on the stack, depending on where they are defined.两个x都将在 static memory 或堆栈中分配,具体取决于它们的定义位置。 However the pointer will be initialized so as to point to the corresponding "Hello World!"然而,指针将被初始化以指向相应的"Hello World!" string, while the array will be initialized by copying the string literal directly into it.字符串,而数组将通过将字符串文字直接复制到其中来初始化。

In theory the compiler is free to reclaim the memory of both string literals when there are no ways to access them;理论上,当无法访问它们时,编译器可以自由地回收这两个字符串文字的 memory; in practice the first one is unlikely to be reclaimed, as usually static memory remains allocated to the program until its termination;在实践中,第一个不太可能被回收,因为通常 static memory 一直分配给程序,直到程序终止; on the other hand if the array is allocated on the stack the second one might even not be allocated at all, as the compiler may use other means to ensure the array is properly initialized, and the array memory will be reclaimed when it goes out of scope.另一方面,如果数组在堆栈上分配,第二个甚至可能根本不分配,因为编译器可能会使用其他方法来确保正确初始化数组,并且数组 memory 将在退出时被回收scope。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM