Thread: Array of object & memory

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    4

    Array of object & memory

    Hi there guys,

    Another quick, and probably easy, question for you.

    if I do the following:

    Code:
    clsClassName[] myObject = new clsClassName[1000];
    Will the program ask for the entire amount of memory required for 1000 objects? Or will it only reserve the memory for each object when I do:

    Code:
    myObject[x] = new clsClassName();
    Any input would be appreciated.

    Thanks

    Rich

  2. #2
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    Will the program ask for the entire amount of memory required for 1000 objects?
    I think ... yes ...

    You are declaring them so what i think that will happen is that the space will be reserved and finally when you initialise one of them then they will be "filled" with a reference...

    But im pretty sure memory will be reserved. Since if it would not be reserved then how would the program later on know when you assign a reference or whatever to it where it should be stored in the memory...

  3. #3
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    I'm no expert but I would think declaring the array would only allocate the same amount of memory as declaring any other variable. As you go
    myObject[x] = new clsClassName();
    More memory would be assigned.

    Then again maybe not.

  4. #4
    Chief Code Coloniser!
    Join Date
    Apr 2005
    Posts
    121
    The first line of code purely allocates memory for the array variable itself, it doesn't allocate memory for every item in the array, you have to do that separately, so the next line of code will allocate more.

    Cheers.

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    28
    Quote Originally Posted by richard_hooper
    Hi there guys,

    Another quick, and probably easy, question for you.

    if I do the following:

    Code:
    clsClassName[] myObject = new clsClassName[1000];
    Will the program ask for the entire amount of memory required for 1000 objects? Or will it only reserve the memory for each object when I do:

    Code:
    myObject[x] = new clsClassName();
    Any input would be appreciated.

    Thanks

    Rich

    This is an "old" post so not sure if you are interested anymore or not..but here goes

    The first line will allocate an array big enough to hold a thousand pointers (since your object is a ref type). It DOES allocate this space all at once since most all languages (I assume C# is included but I would have to check the standard) guarantee that arrays are contiguous. If it waited to allocate space as you accessed it this would most certainly not be the case. So you have a cost of 1000 * sizeof(pointer) which will be the natural word size on most machines, if this means nothing to you then for a 32 bit machine (most x86's except the newest ones or specialty ones) a pointer is 32 bits (or 4 bytes), on a 64 bit machine a pointer is 64 bits (or 8 bytes). Now the second line will actually allocate the space needed for a single array element and deposit the pointer to it into the array.


    Ps. Someone responded saying the first line only allocates space for the array variable itself, this would be correct if you had said clsName[] MyObject; However you have an initializer in there as well so it WILL allocate all the memory the array needs.

    edit: I am perhaps speaking a bit too vaguely here When I say "all the memory the array needs" I mean enough room for 1000 pointers, in this case.


    Mezzano
    Last edited by Mezzano; 05-20-2005 at 08:26 PM.

  6. #6
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    I am 95% sure that C# arrays are not guaranteed to be contiguous, as they are just objects. An ArrayList on the other hand is, and new memory is allocated when the ArrayList grows too big.

    Quote Originally Posted by http://www.awprofessional.com/articles/article.asp?p=25322&seqNum=14
    The elements of an ArrayList are stored in a chunk of contiguous memory. When that memory becomes full, a larger chunk of contiguous memory has to be allocated (usually twice the size) and the existing elements are copied into this new chunk. We call this chunk the capacity of the ArrayList object.
    But I can find no evidence to suggest that an array of ints, for example, is contiguous, and an array seems to have no Capacity property to indicate it is either.

    Edit: and I fail to see how
    Code:
    clsName[] MyObject;
    Would allocate anything.
    Code:
    MyObj o;
    Doesn't allocate anything, until you use the new keyword to create something. Otherwise:
    Code:
    MyObj o;
    o = new MyObj();
    Would allocate twice the memory by your reasoning.
    Last edited by nickname_changed; 05-20-2005 at 10:11 PM.

  7. #7
    Registered User
    Join Date
    May 2005
    Posts
    28
    Quote Originally Posted by stovellp
    I am 95% sure that C# arrays are not guaranteed to be contiguous, as they are just objects. An ArrayList on the other hand is, and new memory is allocated when the ArrayList grows too big.


    But I can find no evidence to suggest that an array of ints, for example, is contiguous, and an array seems to have no Capacity property to indicate it is either.
    First off you are mistaken, by definition an array is contiguous. If you don't believe me feel free to check out this nice article on MSDN concerning data structures in .NET which gives a thorough treatment to Arrays.

    Data Structures in C#

    Quote Originally Posted by stovellp
    Edit: and I fail to see how
    Code:
    clsName[] MyObject;
    Would allocate anything.
    Perhaps I was being a bit too lose with my terminology. When I said allocate in that context I was speaking simply about making space for the variable. Depending on the context this would either be space in an object (on the heap) or in a stack frame. I view any act of reserving space, regardless of its location, to be allocation. Therefore clsName[] MyObject DOES "allocate" (in my sense) space as it reserves a block of memory (4 bytes wide) and allows us to refer to it as MyObject.


    Quote Originally Posted by stovellp
    Code:
    MyObj o;
    Doesn't allocate anything, until you use the new keyword to create something. Otherwise:
    Code:
    MyObj o;
    o = new MyObj();
    Would allocate twice the memory by your reasoning.
    [/QUOTE]


    As to the first piece of code, see above. As to the second, it does in some sense. MyObj o; creates somewhere to store whatever the value of o is. In the case of a local variable this means it is in the stack frame, otherwise it is within some object on the heap, regardless of where it is it IS somewhere and memory has to be reserved for it. The second expression again allocates, this time surely off the heap. Again I think this confusion was due to my strange view of allocation, coming from a C/C++ background. Sorry for the confusion, I will try to be more clear with my terminology in the future


    Mezzano

  8. #8
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Mezzano, thanks for the article, it seems you are quite correct about arrays. Consider it bookmarked

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  2. memory allocation ptr to array? how?
    By kokopo2 in forum C Programming
    Replies: 8
    Last Post: 09-01-2005, 05:06 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Replies: 1
    Last Post: 03-30-2004, 02:57 PM
  5. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM