sizeof matters needing attention

Source: Internet
Author: User

sizeof is an operator (operator) in C/s + + that returns the number of bytes of memory that an object or type occupies. The return value type is size_t and is defined in the header file stddef.h.

sizeof of pointer variables (unlike arrays): equal to the width of the internal address bus select、read of the computer. So in a 32-bit computer, the return value of a pointer variable must be 4 (note that the result is in bytes), and the sizeof result of the pointer variable in the 64-bit system is 8.


Suppose there is a code fragment:

Char *aa; int *bb;
void *cc;

printf ("aa=%d, bb=%d, cc=%d, sizeof int=%d\n", sizeof AA, sizeof BB, sizeof cc, sizeof (int));

On a 64-bit system, the results are 8.


Suppose there is a code fragment:

int a[]={1,2,3,4,5};

int i;

For (I=-1;i<sizeof (a)/sizeof (a[0]); i++)

printf ("%d", a[i]);

What is the number of cycles?

Should be 0 times, the crux of the matter is the sizeof return value type, is a size_t type, and size_t is defined, typedef unsigned int size_t, that is, it is a unsigned int type. Therefore, when executing i<sizeof (a)/sizeof (a[0]), I is converted to the unsigned int, so I is greater than sizeof (a)/sizeof (a[0)) it, all not performing loops.

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.