Question:

How to find the size of a variable without using sizeof() in C++?

by  |  earlier

0 LIKES UnLike

How to find the size of a variable without using sizeof() in C++?

 Tags:

   Report

2 ANSWERS


  1. Make an array of that type, get the addresses of two adjacent elements, cast them to integer, and subtract.  But generally that's runtime, not compile time.


  2. You don't actually have to make an array.  You can make a pointer to the object, and do the same thing.  Since you're only interested in the pointer arithmetic, you don't even need a real object. Something like this could work:

    int a;

    obj b;

    obj *c = &b;

    a = (int)&obj[1] - (int)&obj[0];

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.