Memory Allocation in an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MrPickle
    New Member
    • Jul 2008
    • 100

    #1

    Memory Allocation in an array

    When I make a static array, like so:

    int myArray[100];

    Will the array 400 bytes big, right away? or will it only assign the memory when it's actually needed?

    eg; if I was then to call int myArray[64] = 5; the array would then be 4 bytes.

    I have the same question for dynamic arrays, if I done:
    int *myArray = new int[100];

    Would it then be 400 bytes big, or only assign the memory as needed?
  • vmpstr
    New Member
    • Nov 2008
    • 63

    #2
    In both cases, the memory is allocated immediately. It doesn't wait for you to access it.

    Comment

    • MrPickle
      New Member
      • Jul 2008
      • 100

      #3
      Ok, thanks.

      I just thought that it may try do this to try and save memory.

      Comment

      • vmpstr
        New Member
        • Nov 2008
        • 63

        #4
        In C and C++, the program generally does what you tell it to do.

        If you would like the program to save memory (and sacrifice speed, since you have to check whether to allocate more memory or not), then you have to implement that yourself.

        Comment

        • gpraghuram
          Recognized Expert Top Contributor
          • Mar 2007
          • 1275

          #5
          When u do a static alloction, the memory is allocated when object gets loaded into the memory or when the function stack gets loaded.
          When u do a dynamic allocation the memory is allocated during run time and in bot cases the memory is allocated fully to the size requested for.

          raghu

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Originally posted by MrPickle
            int myArray[100];

            Will the array 400 bytes big, right away?
            This is a minor point and slightly off topic for the question but the size of the array will be 100 * sizeof(int). It sounds like your system has 4 byte ints which would make the result of that calculation 400 bytes but ints are not the same size on all platforms. Plenty of platforms have 2 byte ints and some even have 1 byte ints.

            An int is supposed to be the size that the platform find easiest to process (i.e. most efficient) 1 byte on an 8 bit platform, 2 bytes on a 16 bit platform 4 bytes on a 32 bit platform. In practice int is most often 2 or 4 bytes (some 8 bit platforms still use 2 byte ints).

            In theory the modern 64 bit platforms should have 8 byte ints (and longs) but for some reason most of the compiler manufactures decided to keep ints and longs 4 byte and add the 8 byte long long. IMO this was most likely to prevent breaking all that code out there that assumes long is 4 bytes.

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              if you do 'int myArray[100]' 100*sizeof(int) bytes are reserved for your array which
              could very well be 400 bytes.

              For dynamically allocated memory 'int* myArray= malloc(100*size of(int))'
              things can be a bit more complicated. Most systems allocate all those bytes
              for you immediately (or they fail) but on some (older) systems the memory
              (all 400 bytes) is only allocated when you try to dereference the pointer myArray.

              Notably the old AIX systems worked that way and it could cause havoc because
              you could never tell in advance whether or not the allocation succeeded.

              I don't know of any systems that allocate part of the array on demand.

              kind regards,

              Jos

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                Originally posted by MrPickle
                int *myArray = new int[100];
                The fact that you make this statement tells me you are using C++.

                Do not use arrays in C++. Use the vector container. It is implemented as an array and it allocates memory as needed.

                Comment

                Working...