本文介绍了难道垃圾收集发生时,我们初始化字符串在C文字字符数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们在写C code的以下行,

When we write the following line of code in C,

      char local_arr[] = "I am here";

文字我在这里被存储在内存中的只读部分(比如 RM )。我该如何想象这是它被连续存储在RM(是吗?)。然后阵列的 local_arr (即本地阵列)的副本通过索引此数组索引从它的RM位置。

the literal "I am here" gets stored in the read only part of the memory(say RM). How I visualize it is that it gets stored contiguously in the RM (Is that right?). Then the array local_arr (i.e local array) copies this array index by index from its location in RM.

但之后会发生什么字面在 local_array 的副本呢?难道它失去从而导致内存泄漏?或者是有一些垃圾收集象Java来清理未引用的对象?

But what happens to the literal after the local_array copies it? Is it lost thereby causing memory leaks? Or is there some sort of garbage collector like in Java that cleans up unreferenced objects?

例如,如果我写了一张code如下:

For example if i write a piece of code as follows :

for(int i=0;i<100000;i++)
    char local[] = "I am wasting memory";

我会不会耗尽内存?将每次迭代创建与每个迭代相同的文字的一个新实例?或者将它们都指向相同的文字,因为文字每次的价值是一样的吗?

would I not run out of memory? Will each iteration create a new instance of identical literals with each iteration? Or will they all refer to the same literal since the value of the literal everytime is same?

请问 RM 属于堆内存或堆在一个专门的段?

Does RM belong to the heap memory or a specialized segment in heap?

另外,当地的数组存储在堆栈中,对不对?如果我使用一个动态数组或全局数组。然后会发生什么?

Also the local array is stored in the stack, right? What if I use a dynamic array or global array. What happens then?

推荐答案

C没有垃圾收集,所以如果你忘记释放内存分配与适当的释放器,你会得到一个内存泄漏。结果
虽然有时候一个保守的垃圾收集器像使用,导致很多额外的麻烦。

C does not have garbage collection, so if you forget to deallocate allocated memory with the proper deallocator, you get a memory leak.
While sometimes a conservative garbage collector like the Boehm collector is used, that causes lots of extra headaches.

现在,有四种类型的存储器在C:

Now, there are four types of memory in C:


  • 静态内存:这是从开始到结束有效。它有味道逻辑只读(写作是未定义行为)和可写的。

  • 线程本地内存:类似于静态存储器,但不同的每个线程。这是新发明的东西,像所有的线程支持。

  • 自动记忆:在栈上的一切。它是由留块自动释放。

  • 动态内存:是什么的malloc 释放calloc 的realloc 和要求类似的回报。不要忘了免费 RESP。使用其他合适的释放器。

  • static memory: This is valid from start to end. It comes in flavors logically read-only (writing is Undefined Behavior) and writeable.
  • thread-local memory: Similar to static memory, but distinct for each thread. This is new-fangled stuff, like all threading support.
  • automatic memory: Everything on the stack. It is automatically freed by leaving the block.
  • dynamic memory: What malloc, calloc, realloc and the like return on request. Do not forget to free resp. using another appropriate deallocator.

您的例子使用自动存储器,用于 local_arr 和叶免费实施初始化为所提供的文字为准方式是最有效的。

Your example uses automatic memory for local_arr and leaves the implementation free to initialize it to the provided literal whichever way is most efficient.

char local_arr[] = "I am here";

这可能意味着,特别是:

That can mean, inter alia:


  • 使用的memcpy / 的strcpy ,并把文字变成静态存储器。

  • 通过推动部件,从而把球送入执行的指令构建堆栈上的阵列。

  • 还有别的视为天时地利人和。

  • Using memcpy/strcpy and putting the literal into static memory.
  • Constructing the array on the stack by pushing the parts, thus putting it into the executed instructions.
  • Anything else deemed opportune.

同样的兴趣,C常量文字没有身份,所以可以共享空间。结果
总之,使用as-if规则,多次静态的(甚至是动态/自动)变量可以被优化掉。

Also of interest, C constant literals have no identity, so can share space.
Anyway, using the as-if rule, many times static (and even dynamic / automatic) variables can be optimized away.

这篇关于难道垃圾收集发生时,我们初始化字符串在C文字字符数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 06:51