Typed the same code but different result?

Not sure, where to put it in, but I’ve been following along with FreeCodeCamp tutorial on C with Mike Dane, and I got stuck on the array section

int main()
{

int luckyNumbers[10];
luckyNumbers[1] = 80;
printf("%d", luckyNumbers[0]);

return 0;
}

With that code, Mike got -2 as a result, but I got 8? What is wrong with my code?

You are printing the contents of uninitalized memory. Literally any value might be printed; it’s whatever junk value was in that spot in your computer’s memory.

1 Like

Yeah, judging by the code, I’m guessing that that was the point he was trying to make. Watch that section again and pay close attention. If you still have doubts, please provide a link to the video and a timecode so we can take a look.

1 Like

Hi thank you for the response

1:34:38 mark

I am not to certain about memory and stuff since I haven’t gotten far enough in C to know about it, so it might just me being inexperienced. Thank you in advance for your help

Yeah, I don’t think he phrased that well. He said:

This is going to give me a -2, which basically means that it’s not found.

That is misleading. It makes it sound like “-2” is some special status for “not found”, or at least lets you infer that. It’s not.

When you did:

int luckyNumbers[10];

you were telling C to allocate enough contiguous memory for an array of ints (40 bytes, because and int is 4 bytes, times 10) and store the beginning memory address into luckyNumber.

C is a very low level language. Unlike something like JS, it won’t initialize that for you. It won’t check if it is initialized, it won’t check if what you are trying to access is even within the array (like if you did luckyNumber[127]), tried to change data outside the array, or anything like that. It assumes that you are in control of it. JS babysits you in that respect. C doesn’t care.

What he should have said is:

Because we haven’t initialized the slot at index 0, there is just random data there and C will try to interpret it as an int, in this case it reads it as -2. You may get a different result. The point is that you need to keep track of what data has been initialized.

But then, I’m always suspicious of people that pronounce it “eksetera”. :wink:

2 Likes

Love your explanation man, I only have experience in JavaScript and Python so I never considered the memory allocation thing. Is it possible that the different result is because of a different compiler?

1 Like

I feel the same way about people who pronounce it “mischeevious”.

2 Likes

It’s not dependent upon the compiler. The values in uninitialized memory is just random garbage, whatever happens to be in those memory locations at the time.

2 Likes

I always hate the word “random”. There is the true mathematical sense, but then also sometimes we mean it in the sense of “chaotic” (again in the mathematical sense) but often we just mean unexpected.

I seem to remember when trying this out in C long ago, that I kept getting the same result (not random). We figured that it was because the system was starting up and allocating, using and then deallocating memory, and because it was always following the same steps, the array was getting the same block of memory that had been previously used.

So, the bigger point is that it doesn’t really matter if it is random or deterministic - the point is that you can’t trust it until you’ve put some data there yourself.

2 Likes

As I understand it, what values you get depends a bit more on your exact operating system. It can actually be really, really bad to let some program read any piece of memory it wants, so some OSs sandbox your programs to prevent you from reading sensitive information.

In any case, the important point is that uninitalized memory is full of old/junk values and should not be read before you put some initial values in there.

1 Like

Well, random in the sense that running this piece of code on your machine and on my machine are probably not going to produce the same results. Also, I tested this with indexes toward the end of the array and a few of them had different output every time, as if it was almost random.

1 Like