Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

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



Application Security Testing: An Integral Part of DevOps

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.



We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date