dcsimg
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: C++
Expertise: Intermediate
Jul 28, 2004

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


Determine Object Size Without Using the sizeof() Operator

The sizeof() operator gives you the number of bytes required for storing an object. It also operates on a data type or a value.

Another way of determining the size of an object is to use pointer arithmetic, as in the following example:


struct point {
    long x, y;
};

int main()
{
    struct point pt = {0}, *ppt = &pt
    unsigned char *p1 = NULL, *p2 = NULL;
    size_t size = 0;

    p1 = (unsigned char*)(ppt);
    p2 = (unsigned char*)(++ppt);
    size = p2 - p1; // size is now 8 bytes (2 longs)
    // same as sizeof(struct point) or sizeof(pt)

    return 0;
}
Nahappan Somasundaram
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date