Sizeof computing depends on memory allocation, sizeof Memory Allocation

Source: Internet
Author: User

Sizeof computing depends on memory allocation, sizeof Memory Allocation

This article records some calculations about sizeof, mainly including the following four situations: (if there is an error, please leave a message)

Use sizeof () to calculate the memory space occupied by common variables

# Include <iostream> # include <stdlib. h> using namespace std; // If the array variable is passed into the function for sizeof () operation, it is no different from the pointer operation. Void Func (char str [100]) {cout <"Fun: =" <sizeof (str) <endl; // 4-byte pointer} int main () {char str [] = "Hello"; char * p = str; int n = 10; cout <"str =" <sizeof (str) <endl; // result = 6 = strlen ("Hello") + 1; leave an element in the value to save the string Terminator Func (str); // both results are 4, in win32, the pointer and int are both 4-byte cout <"p =" <sizeof (p) <endl; cout <"n =" <sizeof (n) <endl; // Result 4 void * q = malloc (100 ); // 100 bytes of heap memory cout <"void * =" <sizeof (q) < <Endl; // or 4 (pointer size) char * ch = (char *) malloc (sizeof (char) * 10 ); cout <"char * =" <sizeof (ch) <endl; // or 4 (pointer size) return 0 ;} // The array and pointer sizeof () are different. // The pointer size is 4 bytes regardless of its type.

In the preceding example, we can see that sizeof calculates the space occupied by the data type. In addition, when initializing char str [] = "Hello, sizeof calculates the length of the string plus 1.

Size of the space occupied by the sizeof computing Class Object-byte alignment

# Include <iostream> using namespace std; class A {public: int I; // 4}; class AA {public: double d; int I; // 8 + 4 + 4 (Completion)}; class B {public: char ch; // 1}; class C {public: int I; short j; // 4 + 2 + 2 (Completion)}; class D {public: int I; short j; char ch; // 4 + 2 + 1 + 1 (Completion )}; class E {public: int I; int ii; short l; char ch; char chr; // 4 + 4 + 2 + 1 + 1}; class F {public: int I; int ii; int iii; short j; char ch; char chr; // 4 + 4 + 4 + 2 + 1 + 1}; class FF {Public: short j; int I; char ch; int ii; char chr; int iii; // 2 + 2 (Completion) + 4 + 1 + 3 (Completion) + 4 + 1 + 3 (Completion) + 4 = 24}; int main () {cout <"sizeof (double) =" <sizeof (double) <endl; // 4-byte cout <"sizeof (float) =" <sizeof (float) <endl; // 4-byte cout <"sizeof (int) =" <sizeof (int) <endl; // 4-byte cout <"sizeof (short) = "<sizeof (short) <endl; // 2 cout <" sizeof (char) = "<sizeof (char) <endl; // 1 cout <"sizeof (A) =" <sizeof (A) <endl; cout <"sizeof (AA) =" <sizeof (AA) <endl; // The widest data type is 8 bytes. Cout <"sizeof (B) =" <sizeof (B) <endl; cout <"sizeof (C) =" <sizeof (C) <endl; cout <"sizeof (D) =" <sizeof (D) <endl; cout <"sizeof (E) = "<sizeof (E) <endl; cout <" sizeof (F) = "<sizeof (F) <endl; cout <"sizeof (FF) =" <sizeof (FF) <endl; // The number of the same type, but the order is changed, it is different. Return 0 ;}

Sixeof calculates the space size of class objects containing virtual functions

#include <iostream>using namespace std;class Base{public:    Base(int x):a(x){}    void print()    {        cout<<"base"<<endl;    }private:    int a;};class Dervied:public Base{public:    Dervied(int x):Base(x-1),b(x){}    void print()    {        cout<<"Dervied"<<endl;    }private:    int b;};class A{public:    A(int x):a(x){}    virtual void print()    {        cout<<"A"<<endl;    }private:    int a;};class B:public A{public:    B(int x):A(x-1),b(x){}    virtual void print()    {        cout<<"B"<<endl;    }private:    int b;};int main(){    Base obj1(1);    cout<<"size of base obj1 is "<<sizeof(obj1)<<endl;    Dervied obj2(2);    cout<<"size of Dervied obj2 is "<<sizeof(obj2)<<endl;    A a(1);    cout<<"size of A obj is "<<sizeof(a)<<endl;    B b(2);    cout<<"size of B obj is "<<sizeof(b)<<endl;    return 0;}

Ordinary functions do not occupy memory. If there is a virtual function, it will occupy the memory of a pointer. The reason is that the system uses a pointer to maintain the virtual function table of this class, no matter how many items (the class contains multiple virtual functions) This virtual function will no longer affect the class size.

Sizeof calculates the space size of the virtual inherited Class Object

#include <iostream>using namespace std;class A{};class B{};class C:public A,public B{};class D:virtual public A{};class E:virtual public A,virtual public B{};class F{public:    int a;    static int b;};int F::b = 10;int main(){    cout<<"sizeof(A) =="<<sizeof(A)<<endl;    cout<<"sizeof(B) =="<<sizeof(B)<<endl;    cout<<"sizeof(C) =="<<sizeof(C)<<endl;    cout<<"sizeof(D) =="<<sizeof(D)<<endl;    cout<<"sizeof(E) =="<<sizeof(E)<<endl;    cout<<"sizeof(F) =="<<sizeof(F)<<endl;    return 0;}

Related Article

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: 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.