Public Group

# int size

This topic is 3799 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
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.

##### Share on other sites
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.)

##### Share on other sites
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];}

##### Share on other sites
Quote:
 Original post by RealMarkPone way you can do it is by comparing pointers:

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

##### Share on other sites
Quote:
 Original post by RealMarkPOops, yeah your right. Bad typo on my part. Fix:

That still won't work. Pointer arithmetic between void pointers is verboten.

##### Share on other sites
Quote:
Original post by SiCrane
Quote:
 Original post by RealMarkPone way you can do it is by comparing pointers:

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

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

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

##### Share on other sites
Quote:
 Original post by RealMarkPMy 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.

##### Share on other sites
Quote:
 Original post by y2jsavehow to find the size of int (in c) without using the sizeof operator.

um, why?

##### Share on other sites
Quote:
 Original post by y2jsavehow 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;}

1. 1
Rutin
69
2. 2
3. 3
4. 4
5. 5

• 21
• 10
• 33
• 20
• 9
• ### Forum Statistics

• Total Topics
633430
• Total Posts
3011829
• ### Who's Online (See full list)

There are no registered users currently online

×