Thread: Memory and storage

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    42

    Memory and storage

    Hi everyone,

    i'm going over some theory and I was wondering could someone help clarify that i have the just of it.

    I've come across a question that asks me how many bytes of memory it would take to store an array of 100 integers and an array of 100 character pointers:

    would i be right in saying that the the integer array would be 400 bytes? and the array of character pointers would depend on the architecture of the CPU - a 32bit processor resulting in 4 bytes of storage per pointer and a 64bit processor resulting in 8 bytes per pointer?

    Thank you to anyone who has any input on the subject, really need to get this seared into my brain exam time is just a stones throw away.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I would answer this question with the help of the sizeof operator. You also need to be sure of the exact type of the integer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The standard does not define the exact sizes of types, only that char = 1 byte. Thus, you cannot know exactly how big the size of a type is.
    However, typically an int is 4 bytes, and pointers are typically 4 bytes for 32-bit systems and 8 for 64-bit.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    An array of 100 X requires 100 times the memory than does a single X.

    Both the size of an int and the size of a pointer depend upon processor architecture and implementation (or attributes of host environment).

    There is nothing that limits the size of an int to exactly 4 bytes. The C and C++ standards just specify minimum ranges that an int must support. For an int, the range must be no smaller than [-32767, 32767] and there is nothing that disallows a larger range. This means an int must be at least 16 bits (i.e. 2 bytes, if we assume 8-bit btes).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    42
    Ah Thats great everyone. Elysia i can only presume that they refer to standard integers, it prob would have been otherwise stated but yes i see your point.

    another question i had come across had asked me to draw a diagram to illustrate how a structure would be stored in memory:

    struct mystructure {
    float x,y;
    unsigned char a,b,c;
    long int z;
    struct mystructure *ps;
    };

    so each of the floats (x and y) would represent 4 bytes(32-bits) as opposed to double floats which would be 8 bytes(64-bits)

    each unsigned character(a,b,c) would represent 1 byte(8-bits).

    the long int (z) would be 8 bytes(64-bits)

    and the pointer to the structure would be 4 / 8 bytes (32/64 bits) depending on the processor?

    _______________________
    [ x ][ y ][a][b][c][ z ][ ps ]

    sorry for the poor effort but i didnt know any other way to draw it out lol, i was reading something about padding? is this before or after the most significant element of the structure? most significant i thought would have been the largest?

    Thanks again for all your replies, hopefully this can help others too.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Again, there is a problem here. The size of int and long depends on platform, implementation and processor architecture.
    On Windows 32-bit and 64-bit, int is 4, long is 4.
    On Linux 32-bit, int is 4, long is 4. On 64-bit, int and long are 8 bytes, IIRC.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Use offsetof(3): offset of structure member - Linux man page to print the offset of each field in your structure.

    It will give you a taste of padding and alignment, as it pertains to your current implementation.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    42
    in class we develop on windows OS so i cant imagine they would expect us (in examination conditions) to know about other OS's. thank you for your answer

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > in class we develop on windows OS so i cant imagine they would expect us
    > (in examination conditions) to know about other OS's.
    Regardless, I would expect them to be teaching you the language, not an implementation.

    Otherwise, when you get into the real world, using different compilers on different operating systems, you could find that what you learnt has only local significance.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by RyanLeonard View Post
    in class we develop on windows OS so i cant imagine they would expect us (in examination conditions) to know about other OS's. thank you for your answer
    I disagree.

    Your question concerned the theory. Which means you need to understand the permitted range of possibilities across different equipment types, not just the particular equipment (a windows box) you happen to have available in the lab.

    My home happens to have two bathrooms. If I am asked to describe my house, I will acknowledge that. If I am asked to discuss the range of possible house designs, I will not limit my attention to houses with two bathrooms.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Elysia View Post
    The standard does not define the exact sizes of types, only that char = 1 byte. Thus, you cannot know exactly how big the size of a type is.
    However, typically an int is 4 bytes, and pointers are typically 4 bytes for 32-bit systems and 8 for 64-bit.
    does it actually define a char as a byte? as I understood it, it was just char <= short <= int <= long, with no actual definitions of what any of them are.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, the standard mandates that char shall be one byte. It makes no guarantees of how many bits one byte is, however.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Quote Originally Posted by Salem View Post
    > in class we develop on windows OS so i cant imagine they would expect us
    > (in examination conditions) to know about other OS's.
    Regardless, I would expect them to be teaching you the language, not an implementation.

    Otherwise, when you get into the real world, using different compilers on different operating systems, you could find that what you learnt has only local significance.
    I think he means that the function(macro) you recommended is Linux dependent, an easy assumption, considering the huge "Linux man page" title, and therefore wrong for his Windows based class.

    Explanation to OP: This would be a problem if it were OS dependent, but offsetof() is part of the C standard library. The fact that the page is from the Linux man page can be confusing, but offsetof() is available for any OS with an implementation of the standard library, which includes Windows.

  14. #14
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Also exists on Windows: offsetof (CRT)

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > It makes no guarantees of how many bits one byte is, however.
    The standards do guarantee that CHAR_BIT is at least 8.

    It also guarantees the minimum ranges for short, int, long etc.

    Some of the more obscure DSP chips apparently have CHAR_BIT set to 32 (and by implication, sizeof(int)==sizeof(char)==1)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory issue
    By abraham2119 in forum C Programming
    Replies: 10
    Last Post: 08-18-2009, 10:24 PM
  2. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  3. Replies: 7
    Last Post: 02-06-2009, 12:27 PM
  4. Free Store of memory
    By George2 in forum C++ Programming
    Replies: 6
    Last Post: 11-12-2007, 02:27 PM
  5. True Errors in Memory and Storage
    By Procyon in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 02-07-2002, 07:31 AM