logo

opérateur sizeof() en C++

Le sizeof() est un opérateur qui évalue la taille du type de données, des constantes et des variables. Il s'agit d'un opérateur de compilation car il renvoie la taille de n'importe quelle variable ou constante au moment de la compilation.

tableau de chaînes en programmation C

La taille, calculée par l'opérateur sizeof(), est la quantité de RAM occupée par l'ordinateur.

La syntaxe de l'opérateur sizeof() est donnée ci-dessous :

 sizeof(data_type); 

Dans la syntaxe ci-dessus, data_type peut être le type de données des données, des variables, des constantes, des unions, des structures ou tout autre type de données défini par l'utilisateur.

java analyser la chaîne en int

L'opérateur sizeof() peut être appliqué aux types d'opérandes suivants :

    Lorsqu'un opérande est de type données

Si le paramètre d'un taille de() L'opérateur contient le type de données d'une variable, puis l'opérateur taille de() L’opérateur renverra la taille du type de données.

Comprenons ce scénario à travers un exemple.

 #include using namespace std; int main() { // Determining the space in bytes occupied by each data type. std::cout &lt;&lt; &apos;Size of integer data type : &apos; &lt;<sizeof(int)<< std::endl; std::cout << 'size of float data type : ' <<sizeof(float)<< double <<sizeof(double)<< char <<sizeof(char)<< return 0; } < pre> <p>In the above program, we have evaluated the size of the in-built data types by using the sizeof() operator. As we know that int occupies 4 bytes, float occupies 4 bytes, double occupies 8 bytes, and char occupies 1 byte, and the same result is shown by the sizeof() operator as we can observe in the following output.</p> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/85/sizeof-operator-c.webp" alt="sizeof() operator in C++"> <ul> <tr><td>When an operand is of Class type.</td>  </tr></ul> <pre> #include using namespace std; class Base { int a; }; int main() { Base b; std::cout &lt;&lt; &apos;Size of class base is : &apos;&lt;<sizeof(b) << std::endl; return 0; } < pre> <p>In the above program, we have evaluated the size of the class, which is having a single integer variable. The output would be 4 bytes as int variable occupies 4 bytes.</p> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/85/sizeof-operator-c-2.webp" alt="sizeof() operator in C++"> <p> <strong>If we add one more integer variable in a class, then the code would look like:</strong> </p> <pre> #include using namespace std; class Base { int a; int d; }; int main() { Base b; std::cout &lt;&lt; &apos;Size of class base is : &apos;&lt;<sizeof(b) << std::endl; return 0; } < pre> <p>In the above code, we have added one more integer variable. In this case, the size of the class would be 8 bytes as <strong>int</strong> variable occupies 4 bytes, so two integer variables occupy 8 bytes.</p> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/85/sizeof-operator-c-3.webp" alt="sizeof() operator in C++"> <p> <strong>If we add a char variable in the above code, then the code would look like:</strong> </p> <pre> #include using namespace std; class Base { int a; int d; char ch; }; int main() { Base b; std::cout &lt;&lt; &apos;Size of class base is : &apos;&lt;<sizeof(b) << std::endl; return 0; } < pre> <p>In the above code, the class has two integer variables, and one char variable. According to our calculation, the size of the class would be equal to 9 bytes (int+int+char), but this is wrong due to the concept of structure padding.</p> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/85/sizeof-operator-c-4.webp" alt="sizeof() operator in C++"> <ul> <tr><td>When an operand is of array type.</td>  </tr></ul> <pre> #include using namespace std; int main() { int arr[]={10,20,30,40,50}; std::cout &lt;&lt; &apos;Size of the array &apos;arr&apos; is : &apos;&lt;<sizeof(arr) << std::endl; return 0; } < pre> <p>In the above program, we have declared an array of integer type which contains five elements. We have evaluated the size of the array by using <strong>sizeof()</strong> operator. According to our calculation, the size of the array should be 20 bytes as int data type occupies 4 bytes, and array contains 5 elements, so total memory space occupied by this array is 5*4 = 20 bytes. The same result has been shown by the <strong>sizeof()</strong> operator as we can observe in the following output.</p> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/85/sizeof-operator-c-5.webp" alt="sizeof() operator in C++"> <p> <strong>Let&apos;s consider another scenario of an array.</strong> </p> <pre> #include using namespace std; void fun(int arr[]) { std::cout &lt;&lt; &apos;Size of array is : &apos; &lt;<sizeof(arr)<< std::endl; } int main() { arr[]="{10,20,30,40,50};" fun(arr); return 0; < pre> <p>In the above program, we have tried to print the size of the array using the function. In this case, we have created an array of type integer, and we pass the &apos; <strong>arr</strong> &apos; to the function <strong>fun()</strong> . The <strong>fun()</strong> would return the size of the integer pointer, i.e., <strong>int*</strong> , and the size of the int* is 8 bytes in the 64-bit operating system.</p> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/85/sizeof-operator-c-6.webp" alt="sizeof() operator in C++"> <ul> <tr><td>When an operand is of pointer type.</td>  </tr></ul> <pre> #include using namespace std; int main() { int *ptr1=new int(10); std::cout &lt;&lt; &apos;size of ptr1 : &apos; &lt;<sizeof(ptr1)<< std::endl; std::cout << 'size of *ptr1 : ' <<sizeof(*ptr1)<< char *ptr2="new" char('a'); <<'size ptr2 <<sizeof(ptr2)<< '<<sizeof(*ptr2)<< double *ptr3="new" double(12.78); ptr3 <<sizeof(ptr3)<< '<<sizeof(*ptr3)<< return 0; } < pre> <p>In the above program, we have determined the size of pointers. The size of pointers would remain same for all the data types. If the computer has 32bit operating system, then the size of the pointer would be 4 bytes. If the computer has 64-bit operating system, then the size of the pointer would be 8 bytes. I am running this program on 64-bit, so the output would be 8 bytes. Now, if we provide the &apos;*&apos; symbol to the pointer, then the output depends on the data type, for example, *ptr1 is of integer type means the sizeof() operator will return 4 bytes as int data type occupies 4 bytes.</p> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/85/sizeof-operator-c-7.webp" alt="sizeof() operator in C++"> <ul> <tr><td>When an operand is an expression.</td>  </tr></ul> <pre> #include using namespace std; int main() { int num1; double num2; cout &lt;&lt; sizeof(num1+num2); return 0; } </pre> <p>In the above program, we have declared two variables num1 and num2 of type int and double, respectively. The size of the int is 4 bytes, while the size of double is 8 bytes. The result would be the variable, which is of double type occupying 8 bytes.</p> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/85/sizeof-operator-c-8.webp" alt="sizeof() operator in C++"> <hr></sizeof(ptr1)<<></pre></sizeof(arr)<<></pre></sizeof(arr)></pre></sizeof(b)></pre></sizeof(b)></pre></sizeof(b)></pre></sizeof(int)<<>

Dans le programme ci-dessus, nous avons déclaré deux variables num1 et num2 de type int et double, respectivement. La taille de l'int est de 4 octets, tandis que la taille du double est de 8 octets. Le résultat serait la variable de type double occupant 8 octets.

Sortir

Jasmine Davis enfant
opérateur sizeof() en C++