为什么“垃圾”数据似乎没有意义?

时间:2022-05-10 20:42:52

I have always wondered why garbage data appears to not be meaningful. For clarity, what I mean by "garbage" is data that is just whatever happens to be at a particular memory address, that you have access to because of something like forgetting to initialize a variable.

我一直想知道为什么垃圾数据似乎没有意义。为清楚起见,我所说的“垃圾”是指在特定内存地址发生的任何数据,您可以访问,因为忘记初始化变量。

For example, printing out an unused array gave me this:

例如,打印出一个未使用的数组给了我:

@°õN)0ÿÿl¯ÿ¯ÿ ``¯ÿ¯ÿ  @`¯ÿø+))0 wy¿[d

Obviously, this is useless for my application, but it also seems like it is not anything useful for any application. Why is this? Is there some sort of data protection going on here perhaps?

显然,这对我的应用程序来说是无用的,但它似乎对任何应用程序都没有任何用处。为什么是这样?这里是否有某种数据保护?

2 个解决方案

#1


As you state in your question:

正如您在问题中所述:

... "garbage" is data that is just whatever happens to be at a particular memory address, that you have access to because of something like forgetting to initialize a variable.

...“垃圾”是指在特定内存地址发生的任何数据,您可以访问,因为忘记初始化变量。

This implies that something else used to be in that memory before you got to use it for your variable. Whatever used to be there may or may not have any relation to how you wish to use the variable. That is, most languages do not force memory used for one type of object to be reused for the exact same type.

这意味着在你将它用于你的变量之前,其他东西曾经存在于那个记忆中。无论过去是什么,可能会或可能不会与您希望如何使用变量有任何关系。也就是说,大多数语言不强制将用于一种类型对象的内存重用于完全相同的类型。

This means, if memory was used to store a pointer, and then released, that same memory may be used to store a string. If the pointer value was read out as if it was a string, something that looks like garbage may appear. This is because the bytes used to represent a pointer value are not restricted to the values that correspond to printable ASCII values.

这意味着,如果内存用于存储指针,然后释放,则可以使用相同的内存来存储字符串。如果指针值被读出就好像它是一个字符串,那么看起来像垃圾的东西可能会出现。这是因为用于表示指针值的字节不限于与可打印ASCII值对应的值。

A common way to detect a buffer overrun has occurred in a program is to examine a pointer value and see if it contains printable ASCII values. In this case, the user of the memory as a pointer sees junk, but in this case it is "printable".

在程序中检测缓冲区溢出的常用方法是检查指针值并查看它是否包含可打印的ASCII值。在这种情况下,作为指针的存储器的用户看到垃圾,但在这种情况下它是“可打印的”。

#2


Of course memory is never garbage, unless you make a conscious effort. After all, you are on a deterministic machine, even if it doesn't always seem like it. (Of course, if you interprete arbitrary bytes as text then it's unlikely that you see yourself as ASCII art, although you would deserve it.)

当然记忆永远不会是垃圾,除非你做出有意识的努力。毕竟,你是一个确定性的机器,即使它似乎并不总是这样。 (当然,如果你将任意字节解释为文本,那么你不太可能将自己视为ASCII艺术,尽管你应该得到它。)

That was the reason for one of the worst bugs in history, quite recently, cf. https://xkcd.com/1354/. Where do you live to have missed it?

这就是历史上最严重的错误之一的原因,最近,参见https://xkcd.com/1354/。你住在哪里错过了它?

#1


As you state in your question:

正如您在问题中所述:

... "garbage" is data that is just whatever happens to be at a particular memory address, that you have access to because of something like forgetting to initialize a variable.

...“垃圾”是指在特定内存地址发生的任何数据,您可以访问,因为忘记初始化变量。

This implies that something else used to be in that memory before you got to use it for your variable. Whatever used to be there may or may not have any relation to how you wish to use the variable. That is, most languages do not force memory used for one type of object to be reused for the exact same type.

这意味着在你将它用于你的变量之前,其他东西曾经存在于那个记忆中。无论过去是什么,可能会或可能不会与您希望如何使用变量有任何关系。也就是说,大多数语言不强制将用于一种类型对象的内存重用于完全相同的类型。

This means, if memory was used to store a pointer, and then released, that same memory may be used to store a string. If the pointer value was read out as if it was a string, something that looks like garbage may appear. This is because the bytes used to represent a pointer value are not restricted to the values that correspond to printable ASCII values.

这意味着,如果内存用于存储指针,然后释放,则可以使用相同的内存来存储字符串。如果指针值被读出就好像它是一个字符串,那么看起来像垃圾的东西可能会出现。这是因为用于表示指针值的字节不限于与可打印ASCII值对应的值。

A common way to detect a buffer overrun has occurred in a program is to examine a pointer value and see if it contains printable ASCII values. In this case, the user of the memory as a pointer sees junk, but in this case it is "printable".

在程序中检测缓冲区溢出的常用方法是检查指针值并查看它是否包含可打印的ASCII值。在这种情况下,作为指针的存储器的用户看到垃圾,但在这种情况下它是“可打印的”。

#2


Of course memory is never garbage, unless you make a conscious effort. After all, you are on a deterministic machine, even if it doesn't always seem like it. (Of course, if you interprete arbitrary bytes as text then it's unlikely that you see yourself as ASCII art, although you would deserve it.)

当然记忆永远不会是垃圾,除非你做出有意识的努力。毕竟,你是一个确定性的机器,即使它似乎并不总是这样。 (当然,如果你将任意字节解释为文本,那么你不太可能将自己视为ASCII艺术,尽管你应该得到它。)

That was the reason for one of the worst bugs in history, quite recently, cf. https://xkcd.com/1354/. Where do you live to have missed it?

这就是历史上最严重的错误之一的原因,最近,参见https://xkcd.com/1354/。你住在哪里错过了它?