There's a simple question?
how to print the size of a variable without using functions like sizeof() ?
for example if you were asked to write a program that prints size of int without using sizeof() with a resault as below:
cout<<sizeof(int);

Oh good, another one of those pointless "can you do... without using the obvious... " exercises which some tutors seem to enjoy giving, but which have absolutely no use outside the classroom.

Print the address of two elements of an array of them.

declare two variables a and b then subtract their addresses. That is really a terrible way to do it because there is no guarentee that the two variables will be next to each other -- the compiler is free to put anything it wants between those variables. Using the sizeof operator is the only way to guarentee getting the correct answer.

I ran this for fun.

Is it necessary to cast the addresses as ints before subtracting? I printed both addresses, and saw that they were 4 apart, but &x[1] - &x[0] came out as 1. But when I cast them to ints, it was 4. Why is the cast necessary?

"&x[1]" doesn't the compiler uses sizeof to evaluate this expression?
Since I am not sure of this. I think unions can be used to find the answer

I wrote this program & it seems to work.

#include<stdio.h>
int main ()
{
    union findsize
    {
          unsigned char c[8] ;
          int a ;
    } obj;
    int i, bytecount = 0 ;
    obj.a = -1 ;
    for ( i = 0 ; i < 8 ; i++ )
        if ( obj.c[i] == 0xff )
            bytecount++ ;
    printf ( "%d", bytecount ) ;
    getchar();
    return 0 ;
}

Why is the cast necessary?

Because without the cast the difference is 1 integer. If you take the difference between &x[2] - &x[0] the difference is 2 integers.

> But when I cast them to ints, it was 4. Why is the cast necessary?
If you have
p = &a[n];
then
n = p - a;

For the same reason that doing p++ advances by one unit of whatever the pointer is pointing to, not by 1 byte of memory.
Oh, and casting to unsigned char * would have been better than int.

>>For the same reason that doing p++ advances by one unit of whatever the pointer is pointing to, not by 1 byte of memory.

Does this operation require the use of sizeof operator? & please look at my previous post, I have edited it.

Does this operation require the use of sizeof operator? & please look at my previous post, I have edited it.

That would be compiler dependent -- but probably not.

For the same reason that doing p++ advances by one unit of whatever the pointer is pointing to, not by 1 byte of memory.

Oh, ok...

I suppose internally, the compiler does something like what sizeof does, but it's not something you ever concern yourself with as a programmer.

The compiler needs to know the size of things in many circumstances, so it's use here isn't special or unique.

Allow me to have a share in this entertainment:

union { int i; char c; } trick[2];
	cout << &1[trick].c - &0[trick].c << endl;

Any pointer to int casting is implementation-defined. No guarantee that

int a[2];
(int)&a[1]-(int)&a[0] == sizeof(int)
// also may be false:
(int)&a[1] > (int)&a[0]

Of course usually we can find the size in this way but it doesn't correct always.

It's funny, but it's much more correct solution than all others (incorrect) above.
Moreover, I suspect this solution is corresponding to the language standard.

I don't know why a[0] & a[1] are not assured to be in successive memory locations. But if that is the case, then how would this be assured of correctness?
&1[trick].c - &0[trick].c == &*(1+trick).c-&*(0+trick).c == &trick[1].c - &trick[0].c

And how my code is incorrect? Please explain.

The only valid result from int a[2] is &a[1]-&a[0]==1 . It's not interesting thing (has nothing to do with sizeof(int)).

int main ()
{    
    union findsize 
    {
        unsigned char c[8];
        int a ;    
    } obj;
    // Now we have uninitialized findtype type variable obj.
    // For example, a garbage(!) contents of char[8] member is
    // "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" - why not?
    ...
    obj.a = -1 ;
    ...

In that case this program prints 8 (on 32-bit int platform with sizeof(int) == 4). Next time it prints 4, next time (may be) it prints 5 (and so on)...

Yes, it's a trivial identity in C++ (and C)

&1[trick].c - &0[trick].c == &*(1+trick).c-&*(0+trick).c == &trick[1].c - &trick[0].c
I should like to recall that an indexing for POD types is a commutative operator, but as such this (non?)trivial fact does not bear a relation to sizeof(int). The next statements play a vital part:
1. trick's elements occupy continuous memory (it's an array)
2. both have identical members layout
3. sizeof(int) >= sizeof(char) - for every type, not int only
4. sizeof(char) == 1 - by the language definition definition
5. All union members have the same address - by the language definition, 9.5(3)

In that case this program prints 8 (on 32-bit int platform with sizeof(int) == 4). Next time it prints 4, next time (may be) it prints 5 (and so on)...

Strange I always got 4.
I guess I should have added a for loop.
Updated program

#include<stdio.h>
int main ()
{
    union findsize
    {
          unsigned char c[8] ;
          int a ;
    } obj;
    int i, bytecount = 0 ;
    for ( i = 0 ; i < 8 ; obj.c[i++] = 0 ) ;
    obj.a = -1 ;
    for ( i = 0 ; i < 8 ; i++ )
        if ( obj.c[i] == 0xff )
            bytecount++ ;
    printf ( "%d", bytecount ) ;
    return 0 ;
}

The only valid result from int a[2] is &a[1]-&a[0]==1 . It's not interesting thing (has nothing to do with sizeof(int)).

a[1] = *( a+1 ) which is not a regular addition. The compiler has to do some sort of operation at least similar to sizeof() a+1*sizeof(*a) But as Salem said before, a programmer need not worry about it.

@Salem. Thanks for the link, unfortunately the text file you gave is really big & I am still searching for the answer. I did not find it in 6.5.6..

declare two variables a and b then subtract their addresses. That is really a terrible way to do it because there is no guarentee that the two variables will be next to each other -- the compiler is free to put anything it wants between those variables. Using the sizeof operator is the only way to guarentee getting the correct answer.

well,

We can declare an array of the type we need and get the address of two array items which are side by side.
for example,

int a[2];

cout<< &a[0]<<endl;
cout <<&a[1];

with this we can see the addresses of two items which are side by side and are of same type. the difference should be calculated manually.

Caution: Do not use &a[0] - &a[1] or &a[1] - &a[0] . If you try with this, your program always outputs 1.

Comments
Bumping a thread to simply repeat answers given 18 months ago
This article has been dead for over six months. Start a new discussion instead.