Thread: Bytes

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    19

    Bytes

    how many bytes are occupied by a an array of 100ints, is it 100*8=800, that seems wrong

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    100 * sizeof(int) or sizeof(int [100])
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    19
    yeah thats what i thought, it would depend on the size of the int. But this is what ive been asked

    "in C, how many bytes of memory are occupied by an array of 100ints and an array of 100 character pointers"

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I posted the C answer for the array of ints. It is done similarly for an array of pointers.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    Quote Originally Posted by kris_perry2k
    "in C, how many bytes of memory are occupied by an array of 100ints and an array of 100 character pointers"
    That depends on the architecture (and in some cases, the compiler) that you're working with.

    I've worked on machines where sizeof(int) = 2, and sizeof(char *) = 2; I've worked on others where sizeof(int) = 4 and sizeof(char *) = 4, and one machine where sizeof(char *) = 8... Then there was the compiler for the embedded processor that had sizeof(int) = 4, but sizeof(char *) = 2.

    So there is no single correct numeric answer.
    Insert obnoxious but pithy remark here

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    19
    ok what if it was a windows based sytem using a mirosoft c compiler

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Dave_Sinkula
    100 * sizeof(int) or sizeof(int [100])
    And 100 * sizeof(char*) or sizeof(char* [100]).

    And if you mean ILP32 (Win32), then it's 400 for both examples.
    Last edited by Dave_Sinkula; 01-18-2006 at 07:32 PM. Reason: Closed formatting tag.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It depends on the compiler. Do you have a 16 bit compiler? A 32 bit compiler? See. This is why there is no one number answer that is correct. The true "correct" answer is:

    "It depends on the OS, and compiler."


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > ok what if it was a windows based sytem using a mirosoft c compiler
    Write your own program to print the result of various sizeof calculations.

  10. #10
    Registered User
    Join Date
    Dec 2005
    Posts
    25
    Quote Originally Posted by kris_perry2k
    how many bytes are occupied by a an array of 100ints, is it 100*8=800, that seems wrong
    An integer(default in type) type occupies the whole word length of a computer as it's storage . Hence , for a 16 bit computer(my case) one integer occupies 16 bits or convinently 2 bytes. For an array of 100 integers the memory occupied would be 100*word length of computer (200 bytes in my case).

  11. #11
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    Quote Originally Posted by ramayana
    An integer(default in type) type occupies the whole word length of a computer as it's storage . Hence , for a 16 bit computer(my case) one integer occupies 16 bits or convinently 2 bytes. For an array of 100 integers the memory occupied would be 100*word length of computer (200 bytes in my case).

    I.E. what Quzah already posted.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Page File counter and Private Bytes Counter
    By George2 in forum Tech Board
    Replies: 0
    Last Post: 01-31-2008, 03:17 AM
  2. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  3. Shared class members over Dll Boundary
    By Elysia in forum C++ Programming
    Replies: 19
    Last Post: 11-13-2007, 01:43 PM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  5. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 01:54 PM