bill k
2013-03-07 14:02:11 UTC
1) If one declares a static array, for example ten integers long, but only initializes the first three elements(elements 0, 1, and 2) what will elements 3 to 9 contain?
If we declare an int for example and don't initialized it to anything and then print out the value to the screen we get some "random data". My understanding is that this random data is from programs that were using that memory address to store something and then stopped using it and let the operating system know that the address was free for any other program to use but it just never erased the data, correct?
I tried printing out all the elements and elements 3 to 9 all contained zeros. Does this mean that C automatically initializes uninitialized elements to zero, I would think that they would contain random data just like a single int would? Does this also mean that these logical addresses are reserved for our program?
2) Would there be any point in using malloc() to reserve memory for the ten elements in our static array?
3) If I wanted to make a simple program that lets the user enter as many integers as they wish, abd have these integers stored in a 1D array, then I would need to use malloc() correct? I would make the 1D array one element large and have it "grow" as the user entered elements correct? This would be a dynamic array?
Can someone please write this program or at least give me some pseudo code?
I made up all these questions to gain a better understanding of arrays and memory.