IT Community - Software Programming, Web Development and Technical Support

sizeof() operator

This is a discussion on sizeof() operator within the C and C++ Programming forums, part of the Software Development category; Hui buddies can anybody let me know how can i find size of a variable without using sizeof() operator?...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C and C++ Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 08-13-2007, 11:58 PM
vigneshgets vigneshgets is offline
D-Web Genius
 
Join Date: Mar 2007
Posts: 904
vigneshgets is on a distinguished road
Post sizeof() operator

Hui buddies can anybody let me know how can i find size of a variable without using sizeof() operator?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 09-06-2007, 07:46 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Wink Re: sizeof() operator

Hi all,
As an exercise, I am trying to figure out the size of a long in my
machine without using the sizeof operator. I came up with the
following:

Code:
int size_of_long(){
long i = 1,c = 1;
while(i > 0){
i<<=1;
c++;
}
return c / 8;
}
This works fine but I am afraid that if the code is being run in a
machine where it doesn't use 2's compliment. It will not work so I
came up with another function:

Code:
int size_of_long2(){
long i[2];
return (long)(i+1) - (long)i;
}
This works regardless of what machine the code is running.
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 09-11-2007, 05:10 AM
leoraja8 leoraja8 is offline
D-Web Sr.Programmer
 
Join Date: May 2007
Posts: 194
leoraja8 is on a distinguished road
Post Re: sizeof() operator

The sizeof operator returns the size in bytes of its operand. Whether the result of sizeof is unsigned int or unsigned long is implementation defined—which is why the declaration of malloc above ducked the issue by omitting any parameter information; normally you would use the stdlib.h header file to declare malloc correctly. Here is the example done portably:

#include <stdlib.h> /* declares malloc() */
float *fp;

fp = (float *)malloc(sizeof(float));

The operand of sizeof only has to be parenthesized if it's a type name, as it was in the example. If you are using the name of a data object instead, then the parentheses can be omitted, but they rarely are.

#include <stdlib.h>

int *ip, ar[100];
ip = (int *)malloc(sizeof ar);

In the last example, the array ar is an array of 100 ints; after the call to malloc (assuming that it was successful), ip will point to a region of store that can also be treated as an array of 100 ints.

The fundamental unit of storage in C is the char, and by definition

sizeof(char)

is equal to 1.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Elipsis Operator velhari C and C++ Programming 3 11-23-2007 06:45 AM
Add two numbers without using addition operator Murali C and C++ Programming 0 10-31-2007 08:15 AM
How big is an Object? Why is there no sizeof? prasath Java Programming 1 08-17-2007 11:38 PM
overload subscript operator vigneshgets C and C++ Programming 0 08-14-2007 01:01 AM
Operator overloading in C++ prasath C and C++ Programming 1 07-16-2007 06:51 AM


All times are GMT -7. The time now is 01:42 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0