Sizeof history class for all nature's most comprehensive summary - pretreatment, const, static and sizeof (c)

table of Contents

 

sizeof specific application scenarios

Common data types Size

The size of the space occupied by the object class

The space containing object class virtual function size

The difference between sizeof and strlen

String array to store

Pointer to the string

sizeof(str)/sizeof(str[0])

Commonwealth size calculation

 #pragma pack (x) to force the compiler to modify the alignment


sizeof specific application scenarios

  • Unit view an object of type byte memory space occupied.
  • When a dynamically allocated object, you can let the system know how much memory to allocate.
  • Some types of easy expansion, on some platforms (such as windows, python), create a variable, the variable number stored in memory, as a property directly on the properties of this variable among.
  • Some operands when in use, there will be real-time changes, so you can use real-time observation and recording sizeof memory for the situation.
  • If the object sizeof carried out, or the function parameter is an array, it will directly return sizeof size of its pointer.

Common data types Size

  • int type: 4bit
  • short type: 2bit
  • long type: 4bit
  • char type: 1bit
  • double Type: 8bit
  • bool type: 1bit
  • float type: 4bit
  • Pointer type: 4bit
  • Type String: "char str [] =" hello ";", which add length to the last one (because there is a last "\ n" placeholder symbol exists), (5 + 1 = 6)

In a word, most of them are 4bit, the longest of which is 8bit (double type), and the smallest is 1bit (char and bool).

The size of the space occupied by the object class

We must first understand one thing, the size of the object class is created, not after all the variables will accumulate simple class come, instead of using padded manner calculated to come!

Class size must be an integer multiple of the maximum element size of its internal members (essentially, when data is stored in a class, a member of the first data stored in the offset is 0 place, then, after the data members are It is stored from the starting position after the start of the storage of an integer multiple of the size of a member).

#include <stdio.h>
#include <iostream>

class A
{
public:
	int i;
};

class B
{
public:
	char ch;
private:

};

class C
{
public:
	int i;
	short j;

private:

};

class D
{
public:
	int i;
	short j;
	char ch;

private:

};

class E
{
public:
	int i;
	int ii;
	short j;
	char ch;
	char chr;

private:

};

class F
{
public:
	int i;
	int ii;
	int iii;
	short j;
	char ch;
	char chr;

private:

};

int main()
{
	printf("sizeof(int) = %d\n", sizeof(int));
	printf("sizeof(short) = %d\n", sizeof(short));
	printf("sizeof(char) = %d\n", sizeof(char));

	printf("sizeof(A) = %d\n", sizeof(A));
	printf("sizeof(B) = %d\n", sizeof(B));
	printf("sizeof(C) = %d\n", sizeof(C));
	printf("sizeof(D) = %d\n", sizeof(D));
	printf("sizeof(E) = %d\n", sizeof(E));
	printf("sizeof(F) = %d\n", sizeof(F));
	
	system("pause");
	return 0;
}

Run as follows: 

A class includes: an int-> sizeof (A) = sizeof (int) = 4;

Class B comprising: a char-> sizeof (B) = sizeof (char) = 1;

Class C comprises: an int (4 bytes) +1 short (2 bytes) -> sizeof (C) = 4 + 2 = 6, since 6 is not an integer multiple of 4, therefore, two's complement, to give 8;

Class D comprising: an int (4 bytes) +1 short (4 bytes) +1 char (1 byte) -> sizeof (D) = 4+ (2 + 1) = 7, 7 is not as integer multiple of 4, so that 1 up to give 8;

Class E: 4 + 4 + 2 + 1 + 1 = 12, is an integer multiple of 4, to obtain 12;

Class F: 4 + 4 + 4 + 2 + 1 + 1 = 16, is an integer multiple of 4, to obtain 16;

The determination result, and adding the result to see, which is not an integer multiple of the largest addend, if so, it can, if not, to make up the minimum number, making it an integral multiple of the largest addend.

The space containing object class virtual function size

Normal function does not take up memory, but as long as there is a virtual function, it will occupy a memory size of a pointer (which is a virtual function in the virtual function table contains the reason), so that the number of class has virtual functions, with the equivalent several variables pointer (4 bytes).

Below, two sets of inheritance function is set with virtual functions, without a, Talia very obvious differences:

#include <stdio.h>
#include <iostream>

using namespace std;

class Base
{
public:
	Base(int x) :a(x)
	{
	}

	void print()
	{
		printf("base\n");
	}

private:
	int a;

};


class Derived:public Base
{
public:
	Derived(int x) :Base(x - 1), b(x)
	{
	}

	void print()
	{
		printf("derived\n");
	}
private:
	int b;
};

class A
{
public:
	A(int x) :a(x)
	{
	}

	virtual void print()
	{
		printf("A");
	}

private:
	int a;

};

class B:public A
{
public:
	B(int x) :A(x - 1), b(x)
	{
	}
	virtual void print()
	{
		printf("B");
	}
private:
	int b;
};

int main()
{
	Base obj1(1);
	printf("sizeof(Base obj1):%d\n", sizeof(obj1));

	Derived obj2(2);
	printf("sizeof(Base obj2):%d\n", sizeof(obj2));

	A a(1);
	printf("sizeof(A a):%d\n", sizeof(a));

	B b(2);
	printf("sizeof(B b):%d\n", sizeof(b));

	system("pause");
	return 0;
}

It should be noted that the following is compiled in the x86 environment, the result is in line with expected: 

For the Base, which has an int, so the size of the object is created Base class 4 bytes; for the Derived, which has a type of int, and this class that inherits from Base is over, so add 4 on the basis of Base , i.e., 8 bytes;

Similarly, when A is 4 + 4 (int a virtual function type +) = 8; A then B on the basis of the (at this time is not A virtual function is called, so that the count is not four words of virtual functions section) is also added and a virtual function Int type: 4 + 4 + 4 = 12.

But if it is running in x64-bit:

The result is not the same as before and the (specific reasons, left after check).

Note: if the class contains static variables, static class variables and memory space are stored in the local variable is not present in the memory space together, so that, when the class contains static variables to calculate the use sizeof, you will find no calculate the size of the static variable share.

The difference between sizeof and strlen

  1. It is sizeof operator, strlen is a function definition.
  2. sizeof type can be used as a parameter, such as sizeof (int), but not strlen only and, specifically measured strlen string with strlen (char * a),, the object must be "\ 0" at the end.
  3. When the array as a parameter sizeof is not degraded as a pointer, but the function of the time as a parameter, a pointer to degenerate
  4. sizeof during compilation put out the result calculated (as is the type of operator), but must only be strlen results in operation (as is the length of the string operator).
  5. sizeof when in use, if the latter is of a type, it is necessary parentheses: sizeof (int), but if it is variable, there is no need brackets of: sizeof a, because sizeof is an operator, not a function.

If the pointer to the length of the string is calculated, must strlen,

String array to store

such as:

#include <stdio.h>
#include <iostream>

using namespace std;

int main()
{
	char str[20] = "0123456";
	int a = sizeof(str);
	printf("a:%d\n", a);

	int b = strlen(str);
	printf("b:%d\n", b);

	system("pause");
	return 0;
}

Above disorder occurs, and involves the calculation of the time length of the string, to use strlen.

Pointer to the string

#include <stdio.h>
#include <iostream>

using namespace std;

int main()
{
	const char* ss = "0123456";
	int a = sizeof(ss);
	printf("a:%d\n", a);

	int b = strlen(ss);
	printf("b:%d\n", b);

	system("pause");
	return 0;
}

operation result: 

Use sizeof to calculate it, or are not allowed to, it will be more of a "\ 0" character. 

In a word, sizeof can not be used to calculate the length of the string, it can be used strlen.

sizeof(str)/sizeof(str[0])

This command is the number of all the elements of the calculation str array, the number is not valid to calculate the array elements, we must distinguish clearly!

Commonwealth size calculation

Commonwealth depends on:

  • Size for all members of the size of the space occupied by one of the largest members of the (union unique)
  • An integer multiple of the length of the alignment of data types as a member of the largest member of (and union, struct, class in the same alignment)

See the following code,

#include <stdio.h>
#include <iostream>

using namespace std;

union u0
{
	int i;
	double j;
};

union  u1
{
	char a[13];
	int i;
};

union u2
{
	char a[13];
	char b;
};

int main()
{
	printf("u0:%d\n", sizeof(u0));
	printf("u1:%d\n", sizeof(u1));
	printf("u2:%d\n", sizeof(u2));

	system("pause");
	return 0;
}

Look at u0, inside a double (8), a int (4), union depends on the type of which the maximum storage period, it is clear that the largest type of double, then 8, look at the alignment there is no problem, no problem, is a multiple of 8. 8.

Look u1, which has a char array, a variable, therefore, as a type (size 13), and int (4 bytes), then the maximum to find the type of the stored 13, again, the principle of alignment, the char size (1 byte), int size is 4, and that the union should be an integral multiple of the size 4, so that (13 + 3) = 16, round an integral multiple of 4. (Note that the particularity of the array, and the size of the minimum unit, in other words, to see the time alignment, the minimum unit of the array is a 1 byte char)

Similarly, u2, which is 13, but the maximum number of bytes into a char (1 byte a), 1 is an integer multiple of 13, so 13

Results are as follows:

 #pragma pack (x) to force the compiler to modify the alignment

The above class C out, in accordance with normal operation, the size of C should be 8, due to the alignment (C class includes: an int (4 bytes) +1 short (2 bytes) -> sizeof ( C) = 4 + 2 = 6, since 6 is not an integer multiple of 4, therefore, two's complement, to give 8;). But I use #pragma pack (1), forced to align the minimum unit is set into 1, so that the size of the class, it becomes directly to the variable type class are accumulated on the line!

#include <stdio.h>
#include <iostream>

using namespace std;

#pragma pack(1)

class C
{
public:
	int i;
	short j;

private:

};

int main()
{
	printf("c:%d\n", sizeof(C)); //原来因为要对齐int类型,结果为8
	system("pause");
	return 0;
}

Published 271 original articles · won praise 8 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_17846375/article/details/104930366