1 K or 1 G Array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • atwinix
    New Member
    • Jan 2010
    • 2

    1 K or 1 G Array

    Hello!

    I am running some tests and I need to create arrays of specific sizes. I have scavenged many online tutorials/materials to try to find out how I can generate a 1-kilobyte or a 1-gigabyte array in C (or python) without much success.

    Could anyone please help?

    Gratefully yours,

    atwinix
  • alexis4
    New Member
    • Dec 2009
    • 113

    #2
    1K is 1000000 times smaller than 1G!!!
    Anyway, what exactly are you missing and need help for? I have never tried to create such big arrays before, but is it programmaticall y different to create a 1K array and a 10byte array?

    Comment

    • myusernotyours
      New Member
      • Nov 2007
      • 188

      #3
      The size of char on most systems is generally 1 byte long. One KB = 1024 bytes, so a declaration such as
      Code:
       char kb[1024];
      is exactly one kilobyte long.

      You could do something similar for a GB but you have to be careful about where the memory comes from.
      Local variables will be allocated on the part of memory known as the Stack which is considerably limited on most systems so you could get errors like Stack Overflow if you allocate a very huge local array.

      If you want a GB array you should use the Heap which is bigger. You use the malloc() function to allocate memory on the Heap.

      Kind Regards,

      Alex.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        The size of char on most systems is generally 1 byte long.
        More precisely the size of char on all systems is defined by the standard as being 1 byte long. What is variable is the number of bits in that byte.

        Comment

        • myusernotyours
          New Member
          • Nov 2007
          • 188

          #5
          More precisely the size of char on all systems is defined by the standard as being 1 byte long. What is variable is the number of bits in that byte.
          Thankyou for that enlightment. :)

          Comment

          • atwinix
            New Member
            • Jan 2010
            • 2

            #6
            Thanks for the replies! I actually did something similar. I created char 1k[1024]. But I just wanted to confirm if there was actually any harm in doing so, especially when doing the same for the 1G array. Or if there was a better way of doing it.

            I am mostly a Python/Java programmer. I just got started with C in my internship and I am trying to see if larger arrays will cause memory modules to consume more power. I have to run the c program on the most minimalist linux kernel - enough to compile the c program. The computer I am using is very new and has more than ample memory to hold a 1G array.

            Your input have been very helpful. I shall check malloc() as well to get a better understanding.

            Thanks a lot for all the help.

            Comment

            Working...