Thread: Signed vs Unsigned int

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    3
    Thank you all for your input, It seems clear that an const unsigned int would be the best choice while checking for a segfault error. I had assumed that creating an array of such a large size would cause some kind of an error on most systems but wasn't sure what to do about it.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by osyrez
    I had assumed that creating an array of such a large size would cause some kind of an error on most systems but wasn't sure what to do about it.
    Creating an array of that size isn't only an error for most systems, it's an error for a compiler. You simply can't have an array of that size.
    Code:
    const unsigned int USIGN_MAX = (unsigned int)-1; // The largest unsigned value
    const signed int SIGN_MAX = USIGN_MAX / 2; // The largest signed value
    
    int main() {
       int foo[SIGN_MAX] = { 0 };
       return 0;
    }
    On my computer, SIGN_MAX was 2147483647. Which would make this a 32 bit integer. Now, if you do the math (We'll make it 2147483648 to remain in integers).
    Code:
    2147483648 
       times 4 bytes
       equals 8589934592 bytes
       divided by 1024
       equals 8388608 kilobytes
       divided by 1024 
       equals 8182 megabytes
       divided by 1024
       equals 8 gigabytes of memory
    You can see why this is a problem. Your compile may let you get away with this with a char, but if you plan on initializing it, good luck getting it to run.
    Last edited by SlyMaelstrom; 08-16-2006 at 09:28 PM.
    Sent from my iPadŽ

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Another approach I saw recently was to use some RSIZE_MAX and test against it.
    Quote Originally Posted by n1135
    2 The macro is

    RSIZE_MAX

    which expands to a value6) of type size_t. Functions that have parameters of type rsize_t consider it a runtime-constraint violation if the values of those parameters are greater than RSIZE_MAX.

    Recommended practice

    3 Extremely large object sizes are frequently a sign that an object’s size was calculated incorrectly. For example, negative numbers appear as very large positive numbers when converted to an unsigned type like size_t. Also, some implementations do not support objects as large as the maximum value that can be represented by type size_t.

    4 For those reasons, it is sometimes beneficial to restrict the range of object sizes to detect programming errors. For implementations targeting machines with large address spaces, it is recommended that RSIZE_MAX be defined as the smaller of the size of the largest object supported or (SIZE_MAX >> 1), even if this limit is smaller than the size of some legitimate, but very large, objects. Implementations targeting machines with small address spaces may wish to define RSIZE_MAX as SIZE_MAX, which means that there is no object size that is considered a runtime-constraint violation.
    (page 17 of 89 page pdf)
    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.*

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    3
    Quote Originally Posted by SlyMaelstrom
    Code:
    2147483648 
       times 4 bytes
       equals 8589934592 bytes
       divided by 1024
       equals 8388608 kilobytes
       divided by 1024 
       equals 8182 megabytes
       divided by 1024
       equals 8 gigabytes of memory
    You can see why this is a problem. Your compile may let you get away with this with a char, but if you plan on initializing it, good luck getting it to run.
    ick.... ya I see what you mean.

    So I'm assuming the max size an array can have is determined by how much avaliable memory a system has. If this is the case then one could find out how much memory is avaliable before creation of the dynamic array and determine from that what the maximum size of the array could be. Of course that would leave no room for anything else so that wouldn't be desirable. Now I suppose one could subtract off of that an amount that would leave a reasonable amount of memory for the system to run normaly. But then again that is quite a large chunk of memory to be allocating for one array, and that would also limit the use of the function. Although I'm hesitant to set a limit to a possible array size, it seems like the best option so far. I would just need to figure out what the largest array size I could need for my purpose and maybee add a bit just to be sure.

    On that note I'm thinking of doing somthing of the sort

    Code:
    #define MAX_ARRAY_SIZE  1000
    
    
    function(const unsigned);
    
    int main()
    {
    
    ... return 0;
    } void function(const unsigned size) {
    if (size > MAX_ARRAY_SIZE) {
    throw some_error;
    }
    }
    using #define to set my maximum size for all arrays in the program and allowing my to easily change that value if needed.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    No, no, no, and no... just stop what you're thinking and back up. Forget about checking system resources, system memory, or anything like that... If you're creating an array that could potentially cause system instability it's so large then you are already doing something wrong. Arrays suck... period. They're good for very small things in C++. If you're handling a lot of data, look to containers like vectors and queues. They'll automatically allocate, deallocate, resize everything for you. No need to decide at the beginning how much memory you need or can afford. If your container gets too large, then you can store it in a file on the harddrive (god forbid) and use it like a swap file.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Need Help Please
    By YouShallNotPass in forum C++ Programming
    Replies: 3
    Last Post: 08-22-2006, 11:22 AM
  3. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 07:45 AM
  4. Can't Find Conio.h?
    By drdroid in forum C++ Programming
    Replies: 27
    Last Post: 09-26-2002, 04:24 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM