int size

Started by
12 comments, last by Zahlman 15 years, 9 months ago
how to find the size of int (in c) without using the sizeof operator. i used this and it's working too but i suppose that there is a much short way to do it int a; int *b=&a int d=(int)b; int c=(int)++b; printf("%d",c-d); please help thanks
Advertisement
Quote:i used this and it's working too but i suppose that there is a much short way to do it
Yes, with sizeof, which is a compile time operator, or by looking in the compiler definitions for the size of int before compiling.
printf("%d",(int)(((int*)0)+1));

This, BTW, will work on your computer, but is not guaranteed to work. Why? (Hint: 0 doesn't always mean 0.)
one way you can do it is by comparing pointers:

int getSizeOfInt(){  int foo[2];  return &foo[1] - &foo[0];}


Or you can turn it into a template which could give you the same result but for different types:

template<typename T>int getSizeOf(){  T foo[2];  return &foo[1] - &foo[0];}
------------Anything prior to 9am should be illegal.
Quote:Original post by RealMarkP
one way you can do it is by comparing pointers:

That won't work. The pointer arithmetic you're using will always return 1.
Quote:Original post by RealMarkP
Oops, yeah your right. Bad typo on my part. Fix:


That still won't work. Pointer arithmetic between void pointers is verboten.
Quote:Original post by SiCrane
Quote:Original post by RealMarkP
one way you can do it is by comparing pointers:

That won't work. The pointer arithmetic you're using will always return 1.


My bad, forgot to cast:

return (int)&foo[1] - (int)&foo[0];



EDIT: With the above method, would it not tell you the endian order as well?
------------Anything prior to 9am should be illegal.
Quote:Original post by RealMarkP

My bad, forgot to cast:

*** Source Snippet Removed ***


EDIT: With the above method, would it not tell you the endian order as well?


It will just give you the number in bytes, but no indication of endian-ness. While the underlying bytes may be slightly different, the system is still interpreting it as the same value. Just tried it on the 360 kit at work to make sure and the result is the same.

On the original topic, your far better off using sizeof(). It's compile time, so no overhead involved.
Quote:Original post by y2jsave
how to find the size of int (in c) without using the sizeof operator.

um, why?
Quote:Original post by y2jsave
how to find the size of int (in c) without using the sizeof operator.

In C++, you can use the numeric_limits traits. I'm sure there's an equivalent in C.

#include <iostream>#include <limits>int main(){    int i = std::numeric_limits<int>::digits          + std::numeric_limits<int>::is_signed;    std::cout << "an int consists of " << i << " bits" << std::endl;    int c = std::numeric_limits<char>::digits          + std::numeric_limits<char>::is_signed;    std::cout << "a char consists of " << c << " bits" << std::endl;    int s = i / c;    std::cout << "sizeof(int) == " << s << std::endl;}

This topic is closed to new replies.

Advertisement