The operation ‘sizeof’. The operation ? :

The operation ‘sizeof’. The operation ? :


Content

  1. What is the purpose of the sizeof operation in C++ programs?
  2. Examples of using the sizeof operation for base types and numeric values
  3. How to determine the size of a structured variable? Example
  4. How to determine the size of an array of numbers? Example
  5. How to determine the size of an array of structures? Example
  6. How to determine the size of an object (instance) of a class? Example
  7. How to determine the size of an array of class objects? Example
  8. What is the general form in the program of a ternary operation ‘? :’?
  9. Examples of using the operation ‘? :’

1. What is the purpose of the sizeof operation in C ++ programs?

The sizeof operation is used to determine the size of a data type, a variable of a base type, a variable of a structured type, a numeric value, a string value, and so on.

The operation ‘sizeof’ is useful in cases where you need to dynamically allocate memory for variables. In this case, you need to know the amount of memory that an object of one type or another occupies.

The general form of the operation sizeof:

sizeof(type_or_variable)

where

  • type_or_variable – The name of the data type or variable (object) that is currently used in the program.

 

2. Examples of using the sizeof operation for base types and numeric values

The following code snippet provides an example of determining the size of a base type variable, a numeric value, a string value, or the result of an expression.

// the operation 'sizeof'
int a;
short int b;
float x;
double y;
long double z;
int size;

size = sizeof(b); // size = 2
size = sizeof(int); // size = 4
size = sizeof(a); // size = 4
size = sizeof(x); // size = 4
size = sizeof(double); // size = 8
size = sizeof(z); // size = 8
size = sizeof(long double);
size = sizeof(bool); // size = 1
size = sizeof(true); // size = 1
size = sizeof(28); // size = 4
size = sizeof(9.8 + 5); // size = 8 
size = sizeof("Hello world!"); // size = 13
size = sizeof('\n'); // size = 1

 

3. How to determine the size of a structured variable? Example

Let the structure variable, describing the data about the student, be given. The structure template is described in a separate file “MyStruct_Student.h”:

struct Student
{
    char name[70];
    int age;
    char address[100];
};

Then, to determine the amount of memory that is allocated to a variable of type ‘struct Student’, you can write the following code:

#include "MyStruct_Student.h"

...

// determining the size of a structured variable
int size;
Student ST;

size = sizeof(struct Student); // size = 176
size = sizeof(ST); // size = 176

...

 

4. How to determine the size of an array of numbers? Example
// array size determination
int A[100]; // an array of 100 integers
float X[200]; // an array of 200 real numbers
int size;

size = sizeof(A); // size = 4 * 100 = 400
size = sizeof(X); // size = 4 * 200 = 800

 

5. How to determine the size of an array of structures? Example

Let the structure variable describing the data about the student be given. The structure template is described in a separate file “MyStruct_Student.h”:

struct Student
{
    char name[70];
    int age;
    char address[100];
};

The program code, which determines the size of an array of 100 structural variables of type Student, will look like this:

#include "MyStruct_Student.h"

...

// determining the size of an array of structures
Student A[100]; // array of 100 structures of Student type
int size;

size = sizeof(A); // size = 176 * 100 = 17600

 

6. How to determine the size of an object (instance) of a class? Example

Let the “MyClass.h” module describe a class named MyPoint:

class MyPoint
{
    private: int x, y;

    public:
        int GetX(void)
        {
            return x;
        }

        int GetY(void)
        {
            return y;
        }

        void SetXY(int nx, int ny)
        {
            x = nx;
            y = ny;
            return;
        }
};

To determine the size of the memory that is allocated for an object of this class, you need to write this code:

#include "MyClass.h"

...

// the size of the class object
MyPoint P1;
int size;

size = sizeof(P1); // size = 8
size = sizeof(MyPoint); // size = 8

...

As seen from the result, sizeof() operation determines the amount of memory that is allocated for the variable (the field) of class.

 

7. How to determine the size of an array of class objects? Example

Let the “MyClass.h” module describe a class named MyPoint:

class MyPoint
{
    private: int x, y;

    public:
        int GetX(void)
        {
            return x;
        }

        int GetY(void)
        {
            return y;
        }

        void SetXY(int nx, int ny)
        {
            x = nx;
            y = ny;
            return;
        }
};

A code snippet that determines the size of an array of 20 objects of the MyPoint class will look like:

#include "MyClass.h"

...

// the size of an array of objects of the class
MyPoint P1[20];
int size;
size = sizeof(P1); // size = 8 * 20 = 160

...

 

8. What is the general form in the program of a ternary operation ‘? :’?

A ternary operation ? : can replace conditional branch operator if … else. The general form of the ‘? :’ operation is as follows:

expression1 ? expression2 : expression3

where

  • expression1 – any logical expression, the result of which is the value of true or false;
  • expression2 – an expression that will be evaluated if the value of expression1 = true;
  • expression3 – an expression that will be computed if the value of expression1 = false.

 

9. Examples of using the operation ‘? :’

The following code fragment calculates the minimum value between the two variables a and b:

// operation ? :
// minimum value between two numbers
int a, b;
int min;

a = 15;
b = 8;
min = a > b ? b : a; // min = 8

 


Related topics