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?...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| 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;
} 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;
}
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| |||
| 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. |
![]() |
| Thread Tools | |
| Display Modes | |
| |
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 |