Array Ouptut Question!

Hi,

I was actually trying the arrays lesson for tutorials here.

The following is the code that I typed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include <cstdio>
#include <fstream>
using namespace std;

int myarray [] = {1,2,3,4,5,6,7,7,8,9,10};

void main()
{
	cout << "size of myarray is " << sizeof(myarray);
	for(int i=0; i<=sizeof(myarray); i++)
	{ cout << myarray[i] << endl;
	  //cin.get();
	}

} 


Unfortunately, the following output is completely off;

size of myarray is 441
2
3
4
5
6
7
7
8
9
10
0
0
0
-782838870
782838869
1
1
1
1
1
0
-2
1
-1
-1
0
1933828
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
Press any key to continue . . .


My question is how did myarray[] get a size of 441 in the first place? And where are all these extra numbers coming from?
sizeof(myarray) returns the size of your array in bytes.
If you want the size in elements you should calculate it like: sizeof(myarray)/sizeof(int)
Last edited on
Thank you soo much m4ster. I guess the transition from Matlab to c++ will take a bit of time. :P
That still leaves one question though...


Most of the outputs printed by the loop are 0's and 1's. And some are big numbers.

The question is that how come there weren't 441 outputs if the loop was suppose to go fo rthat long.

Also, the 0's and big numbers like 1933828, are these the memory addresses?
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.

-Albatross
Thanks Albatross,

I understand the memory address part now. But does this mean that there was some random data in that array that is still stored in the memory?


Is there a way we can clear the memory as oppose to having junk stored in it unnecessarily?

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.

Also, the first element of myarray[] (which is "== 1") also should've been in the output. But its not there.

I know java would've output an error when trying to compile the " for loop". Can anyone please exaplin this strange behaviour of C++?


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.

-Albatross
Last edited on
 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.
Hi Blackcoder41,

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 thought using <vectors> might be a possibility..... (or at least it was in Java).
Ok if you are concern about unknown length of data I suggest to use list instead of vector.
http://cplusplus.com/reference/stl/list/


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.


EDIT: BTW I like to share this to you http://cplusplus.com/forum/beginner/17828/
Last edited on
Topic archived. No new replies allowed.