简体   繁体   English

当我们将值重新分配给 char 指针时,内存会发生什么?

[英]What happens with memory when we reassign value to char pointer?

I wonder what happens inside the memory when we do something like this我想知道当我们做这样的事情时,内存中会发生什么

  char *s;
  s = "Text";
  s = "Another Text";

If I'm getting it right, by assigning string to char pointer memory is dynamically allocated.如果我做对了,通过将字符串分配给字符指针内存是动态分配的。 So according to my understanding assignment expression所以根据我的理解赋值表达式

  s = "Text";

equals to等于

  s = (char *) malloc(5); // "Text" + '\0'
  strcpy(s, "Text");

Well, this way we can easily free memory by using那么,这样我们就可以很容易地通过使用释放内存

  free(s);

But... After reassigning same pointer to another value, it allocates new memory segment to store that value.但是......在将相同的指针重新分配给另一个值后,它会分配新的内存段来存储该值。

  s = "Text";
  printf("\n(%p) s = \"%s\"", s, s);

  s = "Another Text";
  printf("\n(%p) s = \"%s\"", s, s);

Output:输出:

  (0x400614) s = "Text"
  (0x400628) s = "Another Text"

That means that address of old value is not accessible to us any longer and we can't free that any more.这意味着我们无法再访问旧值的地址,我们不能再释放它。 Another call to free(s);另一个free(s);电话free(s); will probably deallocate only last memory segment used by that pointer.可能只会释放该指针使用的最后一个内存段。

My question is: If we reassign same char pointer over and over again, does it consume more and more program memory during run-time or that garbage somehow gets automatically freed?我的问题是:如果我们一遍又一遍地重新分配相同的字符指针,它会在运行时消耗越来越多的程序内存,还是会以某种方式自动释放垃圾?

I hope that was enough to demonstrate my problem, couldn't think better example.我希望这足以证明我的问题,想不出更好的例子。 If something's not clear enough please ask for additional clarification.如果有些地方不够清楚,请要求进一步说明。

Your understanding is wrong.你的理解是错误的。 It is just the assignment and it does not allocate any memory.这只是分配,它不分配任何内存。 In your example you assign the pointer with the addresses of the string literals.在您的示例中,您将指针分配给字符串文字的地址。 String literals are created compile time and placed in the read only memory字符串文字在编译时创建并放置在只读内存中

You do now allocate any memory by assigning the pointer您现在确实通过分配指针来分配任何内存

It's not equal to doing a malloc.这不等于做一个 malloc。 What's happening is that the string literal is stored in a read only part of memory.发生的事情是字符串文字存储在内存的只读部分。 And it's not the assignment of a pointer that does the allocation.这不是分配指针的分配。 All string literals in a program are already allocated from start.程序中的所有字符串文字从一开始就已经分配。

It might be worth mentioning that they are not strictly speaking stored in read only memory, but they might be and writing to a string literal is undefined behavior.值得一提的是,严格来说,它们并不是存储在只读存储器中,但它们可能是并且写入字符串文字是未定义的行为。

You cannot and should not call free on a string literal.您不能也不应该在字符串文字上调用 free。 Well, you can, but the program will likely crash.嗯,你可以,但程序可能会崩溃。

  • With no optimization, compiler will reserve two distinct memory space for string literals "text1" and "text2".在没有优化的情况下,编译器将为字符串文字“text1”和“text2”保留两个不同的内存空间。

  • If assignment lines are very consecutive as in your question and if nothing is done after the first assignment line —assuming compiling with optimization— compiler, most probably, will not allocate any space for the first string literal nor will produce any opcode for the first assignment line.如果赋值行在您的问题中非常连续,并且在第一个赋值行之后没有执行任何操作(假设使用优化进行编译),则编译器很可能不会为第一个字符串文字分配任何空间,也不会为第一次赋值生成任何操作码线。

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

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