Related Articles
























































Sizeof() Operator In C/C++

Prashant | Sun, 02 Aug, 2020 | 152

sizeof() Operator in C/CPP

Definition of sizeof() operator

sizeof operator is a unary operator that takes a single operand and the operand can be data type, variables, objects, expressions, and anything that has a size, i.e., that acquires some memory. We will see in the example section usage of sizeof operator which you may have never even though of.

Details of sizeof() operator

Operator typeUnary
Operandsdata types, expressions, variables, objects
OverloadingIt can't be overloaded
PrecedenceSame as other unary operators, after the postfix operators
AssociativityRight to left
UsageSize of the return amount of memory allocated in bytes

sizeof() operator on datatypes

As we know different versions of compilers assign the different amounts of memory to data types. Like, in earlier versions, integers were assigned memory of 2 bytes, whereas in the latest versions it’s 4 bytes. We can easily check that by using the sizeof operator.

#include <stdio.h>

int main()
{
    printf("size of integer data type is: %lu\n", sizeof(int));
    printf("size of float data type is: %lu\n", sizeof(float));
    printf("size of double data type is: %lu\n", sizeof(double));
    printf("size of character data type is: %lu\n", sizeof(char));

    return 0;
}

Output:

size of integer data type is: 4
size of float data type is: 4
size of double data type is: 8
size of character data type is: 1

sizeof operator on variables, expressions

We can also use the sizeof operator to find the size of any variable, expressions. The below program shows how to use the sizeof operator to find the size of any variable.

#include <stdio.h>

int main()
{
    int a;
    char b;
    float c;
    double d;
    long e;
    long long f;
    long long int g;

    printf("sizeof a which is of type int is: %lu\n", sizeof(a));
    printf("sizeof a which is of type char is: %lu\n", sizeof(b));
    printf("sizeof a which is of type float is: %lu\n", sizeof(c));
    printf("sizeof a which is of type double is: %lu\n", sizeof(d));
    printf("sizeof a which is of type long is: %lu\n", sizeof(e));
    printf("sizeof a which is of type long long is: %lu\n", sizeof(f));
    printf("sizeof a which is of type long long int is: %lu\n", sizeof(g));

    return 0;
}

Output:

sizeof a which is of type int is: 4
sizeof a which is of type char is: 1
sizeof a which is of type float is: 4
sizeof a which is of type double is: 8
sizeof a which is of type long is: 8
sizeof a which is of type long long is: 8
sizeof a which is of type long long int is: 8

The below program shows how to use sizeof operator to find the size of any expression,

#include <stdio.h>

int main()
{
    int a = 9;
    char b = 'a';
    float c = 8.7;
    double d = 0.99;
    long e = 12;
    long long f = 13;
    long long int g = 16;

    printf("sizeof a+c which is of type int+float is: %lu\n", sizeof(a + c));
    printf("sizeof c+d which is of type float+doyble is: %lu\n", sizeof(c + d));
    printf("sizeof a+e which is of type int+long is: %lu\n", sizeof(a + e));
    printf("sizeof a+c+f which is of type int+float+long long is: %lu\n", sizeof(a + c + f));

    return 0;
}

Output:

sizeof a+c which is of type int+float is: 4
sizeof c+d which is of type float+doyble is: 8
sizeof a+e which is of type int+long is: 8
sizeof a+c+f which is of type int+float+long long is: 4

If you observe the above program, the sizeof operator tells you about the concept of data type broadening. Did you observed when you added int a with long e, the resultant has the size of 8 bytes which is the same as the size of long. So that means, though you have int as an operand, the result will have datatype as long due to the concept of data type broadening. This is also similar when you added float type c with double type 8, again we saw an instance of data type broadeningsizeof operators help you to validate the concept too.

sizeof operator to find sizeof an object

To find what size of an object is, we can use the sizeof operator easily. Normally the size of an object is the sum of its constituent data members. It's guaranteed that size of an object will never be smaller than that, but what compiler does is that compiler adds padding between data members to ensure alignment requirements of the platform. That's one can easily check that by using sizeof operator.

Below is an example where the object has three data members, one string and two integers. So, the size should have been 8 (depends on compiler, one may check the size of string using sizeof operator) + 4 + 4 but it's 24 found by sizeof operator and that's due to compiler adding padding space.

#include <iostream>
using namespace std;

class student {
public:
    int roll;
    string name;
    int marks;
};

int main()
{
    student st;
    st.name = "xyz";
    st.roll = 1;
    st.marks = 80;
    printf("sizeof student object st is: %lu\n", sizeof(st));

    return 0;
}

Output:

sizeof student object st is: 24

sizeof operator to find array size

We can use sizeof operator to find array size. The array size will be array length* sizeof(datatype).

#include <iostream>
using namespace std;

int main()
{
    int marks[20];
    cout << "sizeof the array marks is: " << sizeof(marks) << endl;
    return 0;
}

Output:

sizeof the array marks is: 80

sizeof operator also helps out to find the length of array very easily. The length of the array will be,

length of array= sizeof(array)/sizeof(datatype)

Below is an example:

#include <iostream>
using namespace std;

int main()
{
    int arr[] = { 12, 76, 45, 10, 5, 1, 6, 17, 89 };
    cout << "length the array arr is: " << sizeof(arr) / sizeof(int) << endl;
    return 0;
}

Output:

length the array arr is: 9

Dynamic memory allocation using sizeof operator

Below is the example where we dynamically allocate memory using malloc() function where sizeof helps defining the size of a single element,

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int* arr = (int*) malloc(sizeof(int) * 20);
 
    for (int i = 0; i < 10; i++)
        arr[i] = i;
 
    if (arr == NULL)
        printf("Memory couldn't be allocated\n");
    else
        printf("Memory got allocated\n");
        
    return 0;
}

 

Related Articles
























































0 comments
Leave a comment