Secret of size_t

Source: Internet
Author: User

Today, I saw a size_t type when I was reading a classic double-stranded table. This type was also seen before, but I didn't care too much about it. I always used it as an integer, however, when I analyzed the code of the double-stranded table, I found a problem, which is a little different from what I understood. However, I ran the code correctly, so I started to find the place where I understood the error, I read it from the beginning and found that the only problem was the size_t type. I searched the internet and explained a lot. Generally, I used it as an int, so I wrote a code to test the Code as follows:

# Include <stdio. h>
Int main ()
{
Size_t A =-1;
Size_t B = 90;
If (A <B)
Printf ("A Bao Chi Bu Bian/N ");
Else
Printf ("A bei Bian Yi Wei Bu mA/N ");
}

The running result is:

[Root @ localhost dry] #./CC
A bei Bian Yi Wei Bu Ma
The debugging result is as follows:

[Root @ localhost dry] # GDB CC
Gnu gdb fedora (6.8-29. fc10)
Copyright (c) 2008 Free Software Foundation, Inc.
License gplv3 +: gnu gpl Version 3 or later This is free software: You are free to change and redistribute it.
There is no warranty, to the extent permitted by law. Type "show copying"
And "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu "...
(GDB) List
1 # include <stdio. h>
2 int main ()
3 {
4 size_t A =-1;
5 size_t B = 90;
6 if (a <B)
7 printf ("A Bao Chi Bu Bian/N ");
8 else
9 printf ("A bei Bian Yi Wei Bu mA/N ");
10}
(GDB) B 6
Breakpoint 1 at 0x80483d3: file cc. C, line 6.
(GDB) Run
Starting program:/home/spexamples/1/5/dry/CC

Breakpoint 1, main () at CC. C: 6
6 if (a <B)
Missing separate debuginfos, use: debuginfo-install glibc-2.9-2.i686
(GDB) print
$1 = 4294967295
(GDB) print B
$2 = 90
(GDB) S
9 printf ("A bei Bian Yi Wei Bu mA/N ");
(GDB) S
A bei Bian Yi Wei Bu Ma
10}
It can be seen that during the compilation process, the value of size_t type will be compiled with its complement code. Therefore, when using size_t data, pay special attention to it, especially when using this type in logical expressions. It may cause serious consequences if you do not pay attention to it.

Note: positive complement: same as the original code; negative complement: the sign bit is 1, and the rest is the original code of the absolute value of the number is reversed by bit, and then the entire number is added with 1

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.