Thread: mixed type / buffer over flow

  1. #1
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335

    mixed type / buffer over flow

    I've tried messing with the code below. I'm not sure why it's returning -7. I have a vague idea and that is that there is a overflow since the range of a char is between -127 and 127 but i don't get how c = -7 is calculated.

    when i change j=1 i get


    c = 35

    when c is 4 i get -114 (this is when the minus numbers start to happen). I just haven't made sense of it.


    Code:
    
    int main()
    {
       int i = 10, j = 7;
       double q = 3.56;
       char c;
    
       c = q * i * j;
       printf("c = %d\n", c);
    
       return 0;
    }

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    You are correct in thinking that you have caused an overflow (not a buffer overflow though)

    q * i * j = 3.56 * 10 * 7 = 249.2 which will be a double. When you try and store that in a char, the value will get cast to an integer (249) and then the computer will try to store that number in the 8-bit char. 249 in decimal is F9 in hex or 11111001 in binary so will fit ok into the variable c.

    The problems start because of the way computers store signed numbers (its called two's complement if you want to do some research). Essentially this is how it works:
    Code:
    Number stored in                    Signed Number
    Register (hex)
    0x00                                                   0
    0x01                                                   1
    0x02                                                   2
    etc
    etc
    0x7E                                                   126
    0x7F                                                   127
    0x80                                                  -128
    0x81                                                  -127
    etc
    etc
    0xFE                                                   -2
    0xFF                                                   -1
    So.. in your case 249 stored as 0xF9 looks to the computer like -7; but 35 stored as 0x23 still looks like 35.

    Be warned though that even if you make c an unsigned char the printf function will cast it to a signed int and you will see exactly the same result
    DavT
    -----------------------------------------------

  3. #3
    Register User andor's Avatar
    Join Date
    Aug 2006
    Location
    Novi Sad
    Posts
    42
    Quote Originally Posted by DavT
    Be warned though that even if you make c an unsigned char the printf function will cast it to a signed int and you will see exactly the same result
    No the result will be 249

  4. #4
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    thanks davt

    just another question why isn't the following casted to a char and then stored as a two's complement?

    Code:
    printf("%c", q * i * j);

  5. #5
    Register User andor's Avatar
    Join Date
    Aug 2006
    Location
    Novi Sad
    Posts
    42
    becouse you dont have explicit cast like this
    printf("%c\n", (char) (q * i * j));

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Buffer Over Flow with string Arrays
    By vlrk in forum C Programming
    Replies: 1
    Last Post: 06-24-2008, 07:16 AM
  2. Segmentation Fault?
    By John_L in forum C Programming
    Replies: 10
    Last Post: 10-02-2007, 08:37 AM
  3. scanf oddities
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 09-22-2007, 01:03 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Erros in Utility Header File
    By silk.odyssey in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2003, 06:17 AM