No... memory addresses look more like this:
0x7fff5fbff73c
What you got was random data that was already in the memory spaces the array got.
And as for why there were no 441 outputs, well, at some point one of the memory spaces HAD to have a terminating null character. As far as I know, all arrays have a terminating null character: your 0 is actually not 0, something else is. I expect that there was an error pitched that your compiler didn't compile your code to object to, but rather it just closed quietly. This is all a hypothesis, though.
Is there a way we can clear the memory as oppose to having junk stored in it unnecessarily?
...yes. When initializing the array, be sure to list its length, then initialize each space to something. It takes a long time, but...
The junk isn't "stored" there, an array is a pointer to that junk. The random data was in the memory until the array required some space, which is when it gave pointers to that junk. That junk can be cleared, but won't impede program operation if you don't unless you don't know what you're doing.
I personally think that the program shouldn't compiled or at least output some error.
Reason being that the number of elements in myarray are 11. Whereas the loop was suppose to run till 440 at least.
You made a mistake while using the for loop.
Also, the array is not 441 bytes long. It's 44. I knew something was off when I saw the value. It seemed far too large, but I was hesitant to call anything wrong. Then, later, I counted 43 outputs and noticed also that there was a 1 missing in the output of the original values, which you pointed out later... then I looked closer, and notice that you just forgot a newline.
Also, the array is not 441 bytes long. It's 44. I knew something was off when I saw the value. It seemed far too large, but I was hesitant to call anything wrong. Then, later, I counted 43 outputs and noticed also that there was a 1 missing in the output of the original values, which you pointed out later... then I looked closer, and notice that you just forgot a newline.
Smart one. I'm very impressed :). And again, thanks a lot.
So, one question this leaves me with.... in java when there is no reference to an object in memory, it is automatically deleted from the memory. Is that not the case in C++?
The reason is that a lot of times you wanna start coding something but you don't always know what the size of array will end up being? My only concern is that random junk being there won't (hopefully) be an unknown/undiscovered barrier when coding large programs. Will it?
So, one question this leaves me with.... in java when there is no reference to an object in memory, it is automatically deleted from the memory. Is that not the case in C++?
According to the Sun's article that I've read before, when you lose a reference to an object and there is no way to access it, the memory occupied by that object is flag and maybe dispose at anytime. The same goes when you assign null to and object. And yeah in C++ you have to free dynamically allocated space manually.
The reason is that a lot of times you wanna start coding something but you don't always know what the size of array will end up being? My only concern is that random junk being there won't (hopefully) be an unknown/undiscovered barrier when coding large programs. Will it?
Since you said you are reading the tutorial here, then I say continue reading until you reach the tutorial on dynamic memory. But don't skip the lesson because it requires understanding on pointers.
Thanks for the reply. I actually read the section on Dynamic memory. Nothing special there other than the two keywords, "new" and "delete", unless I missed some point of the tutorial?
I thought using <vectors> might be a possibility..... (or at least it was in Java).
I believe I understand pointers well enough now. So far, I'm getting the feeling that this memory management wrt pointers can be either very useful, or a curse. Is that correct?
I believe I understand pointers well enough now. So far, I'm getting the feeling that this memory management wrt pointers can be either very useful, or a curse. Is that correct?
In my opinion that is correct, can be useful and can also be a curse.
Sometimes I also forget to delete allocated space space by new, so maybe it depends on the skill of the programmer.