在范围内创建指针时,当指针超出范围时,指向变量的指针会发生什么? [英] When a pointer is created in scope, what happens to the pointed to variable when the pointer goes out of scope?

查看:414
本文介绍了在范围内创建指针时,当指针超出范围时,指向变量的指针会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题说明了一切.

我发现了一个基本相同的老问题,但是我需要一点点澄清

I found an old question that is essentially the same, but I needed a tiny bit further clarification.

在这个问题上,公认的答案是:

In this question the accepted answer says:

char* text = "Hello, world"; 

这里有一个自动变量(指针)是 在堆栈上创建并设置为指向常量内存中的值, 这意味着:

Here an automatic variable (a pointer) is created on the stack and set to point to a value in constant memory, which means:

  • "中的字符串文字在整个程序执行过程中一直存在.
  • 您不负责分配"或释放"它
  • 你可能不会 更改.如果要更改它,则必须分配一些 非恒定内存"并将其复制到那里.
  • the string literal in "" exists through the whole program execution.
  • you are not responsible for "allocating" or "freeing" it
  • you may not change it. If you want to change it, then you have to allocate some "non-constant memory" and copy it there.

这是说指针被删除了,但指针所指向的数据却没有被删除吗? 如果我要在函数中创建1,000,000个指向字符的指针,当它们超出范围时,我的所有内存都将被释放吗?还是只是指针所需的内存,而实际字符本身却留在了我的所有内存中?

Is this saying that the pointer gets deleted, but not the data the pointer is pointing to? If I were to create 1,000,000 pointers to characters in a function, when they go out of scope would all of my memory be released? Or just the memory required to make the pointers, leaving the actual characters themselves behind to hog up all my memory?

推荐答案

字符数组将在程序的整个执行过程中徘徊,因为它们具有静态的存储持续时间.这并不意味着您需要删除它们-它们被假定为在程序的整个过程中都存在.实际上,在其上调用delete会给您带来不确定的行为.您只能deletenew分配的内容.

The arrays of characters will hang around for the entire execution of your program because they have static storage duration. This doesn't mean you need to delete them - they're supposed to stay around for the entire duration of your program. In fact, calling delete on it will give you undefined behaviour. You can only delete something that was allocated with new.

指针本身具有自动存储持续时间,并且在超出范围时会被销毁.值得注意的是,指针必须为const char*,因为字符串文字为您提供了一个const char数组.考虑:

The pointers themselves have automatic storage duration and are destroyed when they go out of scope. It's worth noting that the pointer has to be a const char* because the string literal gives you an array of const char. Consider:

void func()
{
  const char* str = "Hello";
}

包含Hello\0的字符数组在程序运行期间一直存在.指针str仅在该功能期间存在.这里什么都不需要deleted.

The array of characters containing Hello\0 exists for the duration of your program. The pointer str only exists for the duration of that function. Nothing needs to be deleted here.

如果您考虑一下,这很有意义.您在源代码中编写的所有这些字符串都必须存在于可执行文件中.编译器通常将这些字符串写入可执行文件的数据段.运行程序时,可执行文件会与包含字符串的数据段一起加载到内存中.

This makes a lot of sense if you think about it. All of these strings you write in your source code have to exist in your executable somewhere. The compiler usually writes these strings of characters into the data segment of your executable. When you run your program, the executable gets loaded into memory along with the data segment containing your strings.

如果您的程序中有两个具有相同或重叠文本的字符串文字,那么编译器没有理由不能优化它以仅存储其中之一.考虑:

If you have two string literals in your program that have the same or overlapping text, there's no reason the compiler can't optimize it into storing only one of them. Consider:

void func()
{
  const char* str1 = "Hello";
  const char* str2 = "Hello";
  const char* str3 = "lo";
}

在这里,编译器只需要将字符Hello\0写入可执行文件即可.前两个指针将仅指向H,第三个指针将指向第二个l.您的编译器可以像这样进行优化.当然,在此示例中,编译器可以通过完全消除所有字符串来进行进一步的优化-不会以任何有助于程序可观察行为的方式使用它们.

The compiler only needs to write the characters Hello\0 into the executable once here. The first two pointers will just point to the H and the third will point to the second l. Your compiler can make optimizations like this. Of course, with this example, the compiler can make an even further optimization by just getting rid of the strings all together - they're not used in any way that contributes to the observable behaviour of the program.

是的,如果您有百万个不同的字符串文字,它们在某种程度上有助于程序的可观察行为,那么它们当然必须作为可执行文件的一部分存在.

So yes, if you have a million distinct string literals that in some way contribute to the observable behaviour of the program, of course they have to exist as part of your executable.

这篇关于在范围内创建指针时,当指针超出范围时,指向变量的指针会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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