Jump to content
  • Advertisement
Sign in to follow this  
y2jsave

int size

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!