Memory size of base data type __c++

Source: Internet
Author: User

The memory size of the base data type in C + +


C in the different data types of the size of the byte, in different operating systems and compilers, is different, generally speaking of GCC, 32-bit or 64-bit system, the red part of the 32 and 64-bit systems are different, made a table as follows:

Data type

32-bit

64-bit

Value Range (32-bit)

Char

1

1

-128~127

unsigned char (when used in byte)

1

1

0~255

Short Int/short

2

2

–32,768~32,767

unsigned short

2

2

0~65,535

Int

4

4

-2,147,483,648~2,147,483,647

unsigned int

4

4

0~4,294,967,295

Long Int/long

4

8

–2,147,483,648~2,147,483,647

unsigned long

4

8

0~4,294,967,295

Long Long Int/long long

8

8

-9,223,372,036,854,775,808

~9,223,372,036,854,775,807

Pointer

4

8

Float

4

4

3.4E + + (7 digits)

Double

8

8

1.7E/+ 308 (digits)


Attention:

1. When calculating the size of an array, if the array is passed as a parameter, it is degraded to a pointer, so the sizeof (ARR) size is the size of the pointer under the system

2. For C-strings, it is necessary to keep in mind that a Chinese character in C + + is two bytes (3 bytes under Linux).

3. struct or class size, in addition to the basic data type size, especially to consider the problem of byte alignment, if it is C + + also involves virtual function of the virtual table problem, need to add the size of the virtual table pointer, on the issue of memory alignment in other blog will be detailed.

About arrays, array pointers, pointer arrays slightly mentioning:

About sizeof to find the size of the array, the code went to GCC to verify the following, description:

An array pointer, essentially a pointer to an array, regardless of the type, a few dimensional array, sizeof return is the size of the pointer;

Only a normal array int arr[10],int ARR[10][10] is created so that sizeof (ARR) asks for the size of the array, in the following figure, in the fun function, the size of sizeof (a) is the size of the pointer, and the array is degraded to a pointer as a parameter.

As you can see in the following figure, GCC warns that sizeof will only return the value of an int pointer

In the following figure code, similar

int (*p) [10]; P is a pointer to a one-dimensional array of int [10]

INT*P[10]; P is a one-dimensional array of pointers, and each element of the array is a pointer

The results are as follows:

Explained here, because it is a 64-bit system, so the size of the pointer is 8, here a,j is the array pointer, and then D,F is a one-dimensional array, a two-dimensional array of pointers, G,h is also, below explain a wave of their differences. Only B,c is an array of pointers.

In addition to outputting their memory size, there is also the value of the pointer to the address they are pointing to.

Here you can see, a,b,c,,d,e,f their address, are 19335XXX, you can see that they are in a piece of memory, which is the local variable in the stack allocated memory, and G,h is b470xx, because the malloc is dynamically allocated, is allocated in the heap area, so is another piece of memory. and the B,c address difference is 240, the difference is the size of the B array.

Incidentally, the stack area is growing downward and can be verified (below is purely personal interest to see the starting address of different types of arrays in the stack area):


The addresses of the four arrays are contiguous, and the B and arr are growing downward.

Looking at this, after the last two-dimensional array arr2 into a one-dimensional array, the ARR2 address is followed by the ARR (the same type is up), and after the type is inconsistent, the other address is a low point address.

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.