Question:
C programming arrays memory?
bill k
2013-03-07 14:02:11 UTC
Hello. I would appreciate it if someone could clear up a few a things about arrays and memory.

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.
Three answers:
llaffer
2013-03-07 14:14:16 UTC
1) What will elements 3-9 contain? Garbage. You may have been lucky in that it used memory that wasn't used by anything before since you've boot, but a C compiler will not auto-fill new data with zeros. You cannot trust the value of anything you don't initialize.



2) You'd use malloc if you were creating a dynamic array. If you're making a static one, then there is no point in using it and let the compiler handle it the way you did in #1.



3) In C, there really is no method to increasing the size of an existing array. If you create an array in memory, then allocate more memory, there is no way to know that it is adjacent to use array notation. Your best bet would be to make a new array each time, 1 int larger than the last, copying the contents of the old one into the new as you add more values. Don't forget to deallocate the previous array, or you'll create a memory leak in time.



The other solution is to create a linked list structure. Each iteration of the structure contains a pointer to the next element in the chain (and sometimes the previous one if you use double-linked lists. Then you can just create one new structure to store new data, make sure your pointers are right, and you're good. (again, you'll want to deallocate when done).
gene_frequency
2013-03-07 16:24:57 UTC
The other poster is correct - the C standard does not guarantee initialization of memory to zero.



However, that said, some compilers, do zero out variables/allocated memory by default! Additionally, I think on some other compilers you can enable that initialization with a switch/setting.



But we can never assume memory will always be initialized to zero. If it must be zeroed out, add it to your code. Then you will have no issue porting your code to another compiler, or trying to remember if it was important or not when u come back to your code. (You'd be surprised what u forget).
anonymous
2016-08-06 07:03:06 UTC
It can be a quirk, and one you just need to reside with. A 'char *' (pointer to personality) is treated as a string by means of many services, together with the << operator of C++ streams. There is no such designated handling for 'int *'. Recognize that 'char *' (pointer to personality) is the specified case right here. C++ uses pointers to characters as a string class on account that C did and that assemble is still very original.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...