+ 3

Empty Array and Their Values

In this program, I created an empty array, when I print their values, I don't get nil/null. https://code.sololearn.com/cq5pWMHE636d/?ref=app

28th Feb 2018, 4:23 AM
Mr. Potter
Mr. Potter - avatar
18 Antworten
+ 7
lol seems that you have a little trouble.. when iterating the last cycle sum[4] = arr1[4] + arr2[4] well arr1 is composed by 4 elements so arr1[4] is out of the array => undefined behaviour! you can literally crash whit such a code! One solution to avoid this kind of undefined behaviors is use sizeof(array )/sizeof (type) to get array bounds. plus arrays should be initialized /worked/iterated etc etc in a symmetric way.. there should be always te same form.. for example.. int elementsNo=10; arr1[elementsNo]; //declaration arr2[elementsNo]; for(int i =0; i <elementsNo ; ++I){ arr1[i]=0; // initialize arr2[I]=0; } or instead arr1[]={1,2,3,4.....but dangerous.. you can accidentally miss a number => array size is not as expected => undefined behaviour is all up to you.. c++ gives you freedom flexibility but responsibility!
28th Feb 2018, 6:35 AM
AZTECCO
AZTECCO - avatar
+ 7
yes! this is the problem! often compilers doesn't give you any error especially in case of undefined behaviors. Those are the bugs most hard to track down. Don't worry and keep on doing your experiments, arrays.. pointers.. is very important that you do these tests, you will get a robust understanding. after you will learn styles , conventions and a coding methodology to avoid these troubles. Like the little advices I gave you. But for now these experiments gives you a great experience!
28th Feb 2018, 7:06 AM
AZTECCO
AZTECCO - avatar
+ 5
It's garbage, not a random number. You printed what some other process left on the address your array is now.
28th Feb 2018, 4:36 AM
Alex
Alex - avatar
+ 3
Your new program acts like address of arr_one[4] is pointing to i, since i was 4 during the addition and is 5 after the loop ends.
28th Feb 2018, 5:14 AM
John Wells
John Wells - avatar
+ 2
No, but the array is at an address and reads the values out of it. If there is no value assigned it just prints what's in there, since it doesn't get overwritten automatically. BTW: static variables get overwritten with 0 when creating them, so static int x = 0 would be redundant (but you should still do it for readability ^^)
28th Feb 2018, 4:39 AM
Alex
Alex - avatar
+ 2
Nope. The value that's written at the position your array is. And because you didn't tell the compiler what values these are it just takes the values that some other process left there. Computers are dumb. The just do what they are told to do.
28th Feb 2018, 4:45 AM
Alex
Alex - avatar
+ 2
I'll try in Codeblocks
28th Feb 2018, 5:01 AM
Mr. Potter
Mr. Potter - avatar
+ 2
Just to check whether arr_one[4] really pointed to i, I added the following line : cout<<&i<<" "<<&arr_one[4]<<endl; But now, the addresses became different, and the value of arr_one[4] became a random garbage value, and it retained its value (no more increment). But commenting out the line above made arr_one[4] a 5 again in the end. Why?
28th Feb 2018, 5:22 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 2
Pointers are assigned random addresses of memory when declared. That's why many/most of the time they should be initialized. Whatever so happens to be in the memory address it points to will be displayed if not initialized. e.g. assigned value 0,1, or null. You declared an array with certain size then looped through the memory locations they POINT to and displayed what's there. Remember, arrays are actually pointers.
28th Feb 2018, 5:42 AM
Ammon Miranda
+ 2
@AZTECCO C++ doesn't give error. and it prints the result with no elements defined in array position. so I posted the question. I still need to learn more and more...
28th Feb 2018, 6:42 AM
Mr. Potter
Mr. Potter - avatar
+ 2
these are garbage values that are already in a new made array do when u give the value of elements like A[1]=1; then u are just replacing the garbage values
28th Feb 2018, 8:14 PM
Arun
+ 1
So Alex what is printed is the address of the elements.
28th Feb 2018, 4:42 AM
Mr. Potter
Mr. Potter - avatar
+ 1
Ye, So I created another program (with problems) @Alex, @Jayden Take a look https://code.sololearn.com/csd1nzIK93o7/?ref=app
28th Feb 2018, 4:49 AM
Mr. Potter
Mr. Potter - avatar
+ 1
@Mr. Potter I tried printing inside the loop and arr_one[i] gave me the values : 1,2,3,4,4. Maybe its a bug here, so Ill try it on CppDroid. Edit : Same output in CppDroid. Its not a bug, atleast.
28th Feb 2018, 4:59 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 1
I just moved the arr_two to the heap and then, I get a garbage value. But that value remains the same, inside and outside the loop. Thus, I think arr_one overlapped on arr_two. But I still can't guess why I got different values inside and outside the loop.
28th Feb 2018, 5:06 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 1
Thank you swim for rexterster.com
1st Mar 2018, 2:03 PM
Mr. Potter
Mr. Potter - avatar