Like Tree9Likes

Concepts of memory

This is a discussion on Concepts of memory within the C++ Programming forums, part of the General Programming Boards category; Actually, the standard is even weirder than that, specifying (in [basic.fundamental]p2): "There are five standard signed integer types : “signed ...

  1. #16
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Actually, the standard is even weirder than that, specifying (in [basic.fundamental]p2): "There are five standard signed integer types : “signed char”, “short int”, “int”, “long int”, and “long long int”. In this list, each type provides at least as much storage as those preceding it in the list."
    The fun thing? The standard never defines what "providing storage" means. So it's actually somewhat open to interpretation. I would personally actually go more with the sizeof interpretation than the value range interpretation.
    I don't have the C standard to check what that says, though.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  2. #17
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by grumpy View Post
    Not true.
    ....
    The standard only requires that the set of values that can be represented by a char is a subset of the values that can be represented by a short, which in turn is a subset of the values that can be represented by a short .....
    How are those two different ?
    Can you give an example? (Sticking to integral types)
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #18
    Registered User
    Join Date
    Jun 2005
    Posts
    6,245
    Quote Originally Posted by CornedBee View Post
    The fun thing? The standard never defines what "providing storage" means. So it's actually somewhat open to interpretation. I would personally actually go more with the sizeof interpretation than the value range interpretation.
    I don't have the C standard to check what that says, though.
    The C standard only talks about the ranges of values that can be represented. It does not make reference to sizeof() at all in discussing storage of a value in an integral (or any other) type.

    Part of the ambiguity is that the word "storage" can mean "representing a value using a variable" as well as "allocating memory".
    Right 98% of the time, and don't care about the other 3%.

  4. #19
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by grumpy View Post
    Not true. In practice it usually works out that sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long). However, that is not actually a requirement in the standard.

    The standard only requires that the set of values that can be represented by a char is a subset of the values that can be represented by a short, which in turn is a subset of the values that can be represented by a short .....
    Regardless of how numbers are represented, fundamental mathematics seems to imply that if the above statement is true, then the statement about sizeof() is also true. You can't have sizeof(short) < sizeof(char) if short can represent every value that char can represent.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #20
    Registered User
    Join Date
    Jun 2005
    Posts
    6,245
    It is common implementation choices that lead to the rule of thumb about sizeof(), not "fundamental mathematics" or laws of physics.

    All that can be implied from the implementation limits is minimum number of bits needed to represent a type (and that is assuming a binary computer - a ternary computer does not work with bits). The is no "fundamental mathematics" that implies a lower bound (on size of a given type, in this case) is equal to the upper bound.
    Right 98% of the time, and don't care about the other 3%.

  6. #21
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by grumpy View Post
    It is common implementation choices that lead to the rule of thumb about sizeof(), not "fundamental mathematics" or laws of physics.

    All that can be implied from the implementation limits is minimum number of bits needed to represent a type (and that is assuming a binary computer - a ternary computer does not work with bits). The is no "fundamental mathematics" that implies a lower bound (on size of a given type, in this case) is equal to the upper bound.
    Which is why the statement about sizeof() uses '<=' and not '<'. I do not follow you. How can sizeof(short) < sizeof(char) if short can represent everything char can represent?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #22
    Registered User
    Join Date
    Jun 2005
    Posts
    6,245
    Quote Originally Posted by brewbuck View Post
    How can sizeof(short) < sizeof(char) if short can represent everything char can represent?
    It can't, since sizeof(char) is defined to be 1, and sizeof(any other complete type) is defined to a positive value.

    But that's got nothing to do with there being a one-to-one relationship of "supporting a larger range mandates a bigger variable".

    In response to a debate like this, a colleague implemented a bare bones C compiler that had sizeof(short) equal to 4 and sizeof(int) equal to 2, and enforced ranges of values on each type. He then challenged the team to find any clauses in the C standard that would make that compiler non-conformant. The team could not find such clauses.
    Last edited by grumpy; 12-07-2012 at 03:05 PM.
    King Mir and stahta01 like this.
    Right 98% of the time, and don't care about the other 3%.

  8. #23
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,536
    In other words, you can't rely on the sizes of types being greater or equal. One instead has to rely on the set of all representable numbers being greater or equal for every type.
    So, if U is the set of all representable numbers, then

    U(char) <= U(short) <= U(int) <= U(long) <= U(long long)

    Still, one has to wonder what sorts of expensive checks one has to do to enforce an upper bound on a type.
    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.

  9. #24
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by grumpy View Post
    In response to a debate like this, a colleague implemented a bare bones C compiler that had sizeof(short) equal to 4 and sizeof(int) equal to 2, and enforced ranges of values on each type. He then challenged the team to find any clauses in the C standard that would make that compiler non-conformant. The team could not find such clauses.
    Ok, I get it now.

    In theory, if you changed all your ints to shorts in an attempt to save space you might actually use more space. In practice, I wouldn't lose sleep over the possibility.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #25
    Registered User
    Join Date
    Oct 2006
    Posts
    2,348
    actually, while we're at it, what about an implementation of C that runs on an analog computer? I think that's where the "range of values" interpretation would really take on significance, since we'd be talking about a gradient of voltage levels, rather than combinations of ones and zeroes.

  11. #26
    Registered User
    Join Date
    Apr 2006
    Posts
    2,023
    The property of being able to have a discrete type is inherently digital; by having an integer type a computer is necessarily digital. You can have a programmable analog device, but you can't use c++ to program it. You'd need to use Verilog-A or invent something new.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  12. #27
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by Elkvis View Post
    actually, while we're at it, what about an implementation of C that runs on an analog computer? I think that's where the "range of values" interpretation would really take on significance, since we'd be talking about a gradient of voltage levels, rather than combinations of ones and zeroes.
    I'm having trouble imagining what sizeof would actually do on such an architecture. Or what a pointer would mean. I don't think C maps to analog.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  13. #28
    Registered User
    Join Date
    Jun 2005
    Posts
    6,245
    Quote Originally Posted by brewbuck View Post
    I'm having trouble imagining what sizeof would actually do on such an architecture. Or what a pointer would mean. I don't think C maps to analog.
    On such an architecture, sizeof() could be 1 for all basic integral types (char, int, long, unsigned, etc) since the physical manifestation of a variable would be a single voltage line.

    Implementing formatted I/O on such an architecture would be highly entertaining though.
    Elkvis likes this.
    Right 98% of the time, and don't care about the other 3%.

  14. #29
    Registered User
    Join Date
    Oct 2006
    Posts
    2,348
    Quote Originally Posted by King Mir View Post
    The property of being able to have a discrete type is inherently digital; by having an integer type a computer is necessarily digital. You can have a programmable analog device, but you can't use c++ to program it. You'd need to use Verilog-A or invent something new.
    the only reason you "can't" use C++ to program an analog device is that it has simply never been done. I don't think the language has anything in it (other than bitwise operators, and they would be largely irrelevant) that would preclude its use with an analog device.
    Last edited by Elkvis; 12-10-2012 at 06:34 AM.

  15. #30
    Registered User
    Join Date
    Apr 2006
    Posts
    2,023
    Quote Originally Posted by Elkvis View Post
    the only reason you "can't" use C++ to program an analog device is that it has simply never been done. I don't think the language has anything in it (other than bitwise operators, and they would be largely irrelevant) that would preclude its use with an analog device.
    You can use a device that's part analog, but to read a value as an integer requires breaking it down into discrete voltage level, a process that would make the device digital, at least in that part. So what you'd have is digital device that uses modulo N instead of 1's and 0's, for it's most basic unit of information.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Some basic C++ concepts
    By tx1988 in forum C++ Programming
    Replies: 12
    Last Post: 09-15-2007, 08:16 PM
  2. Same Language = two concepts
    By swgh in forum C++ Programming
    Replies: 1
    Last Post: 03-09-2006, 05:28 PM
  3. Need help with concepts for first game
    By blankstare77 in forum Game Programming
    Replies: 29
    Last Post: 10-01-2005, 08:53 AM
  4. Programming concepts
    By lyx in forum C++ Programming
    Replies: 2
    Last Post: 12-02-2003, 11:37 PM

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