Thread: Signed vs Unsigned int

  1. #16
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    That makes sense. You are right that unless it is necessary for some reason to use all the space at once, the use of the container would have that benefit. I was referring to the situation where it all had to be allocated at one time. Usually that's not the case, though.

  2. #17
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    One could check if the system memory still allowed further allocation, otherwise swap to a temp file. But operations on files are tricky, to say the least. You'd need to implement caching for virtual memory, reading chunks of file data into RAM and out into files again. Unless of course the OP isn't planning on using random access on the container.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  3. #18
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    The original issue does remind me of a question.

    Why is that the decision was made to "wrap" out of range unsigned values, instead of generating a compiler error?
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #19
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    1) This assumes that the calling code might pass a negative value, which is pretty blatant abuse when you consider documentation and prototypes that explicitly state that an unsigned value is expected.

    2) If you take a signed value and receive a negative, you should either throw an exception or return an error code. Don't ever terminate the program from a utility function. It's the job of the calling code to determine the severity of an error and act accordingly. Silently working with garbage data is generally frowned upon also.

    >3. Any other suggestions/ideas
    Use an object that doesn't require the calling code to know enough about the internal mechanisms to screw things up.
    My best code is written with the delete key.

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