Like Tree1Likes

Memory and storage

This is a discussion on Memory and storage within the C++ Programming forums, part of the General Programming Boards category; Hi everyone, i'm going over some theory and I was wondering could someone help clarify that i have the just ...

  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
    21,744
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    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,307
    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%.

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,551
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,551
    > 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.
    Elysia likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,307
    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%.

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    2,421
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    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
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    Also exists on Windows: offsetof (CRT)

  15. #15
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,551
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 1 of 2 12 LastLast
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, 11:27 AM
  4. Free Store of memory
    By George2 in forum C++ Programming
    Replies: 6
    Last Post: 11-12-2007, 01: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, 06:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21