2

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?

jxh
  • 69,070
  • 8
  • 110
  • 193
user3424612
  • 183
  • 1
  • 1
  • 9
  • Why do you think it's useless? It's not meaningful as a string, but it probably wasn't a string. – user2357112 May 04 '15 at 23:27
  • 2
    You assume that 'useful data' is in plain text. Integers, floating-point numbers, pointers, etc. are certainly useful, but trying to read them as ASCII text will get you garbage. – Colonel Thirty Two May 04 '15 at 23:27
  • 2
    Because you're forcing it to interpret some random data as characters. A 32 could be a 32, or a space, or anything else in another encoding. Data is only meaningful in the correct representation (thus type systems). – Carcigenicate May 04 '15 at 23:31

2 Answers2

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.

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".

jxh
  • 69,070
  • 8
  • 110
  • 193
1

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.)

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?

Peter - Reinstate Monica
  • 15,048
  • 4
  • 37
  • 62