sizeof related in c/c++
-
hi all, i am sorry i dont know weather i could ask this question ornot coz, these r the thinks always run in my mind. since we call sizeof as operator but y does we use it as sizeof(type) as a function... plese if any buddy finds it silly forgive me i even tried with google i didnt find it out..
-
hi all, i am sorry i dont know weather i could ask this question ornot coz, these r the thinks always run in my mind. since we call sizeof as operator but y does we use it as sizeof(type) as a function... plese if any buddy finds it silly forgive me i even tried with google i didnt find it out..
well ... technically speaking ... every operator IS a function. So what's the difference ?
2 bugs found. > recompile ... 65534 bugs found. :doh:
-
hi all, i am sorry i dont know weather i could ask this question ornot coz, these r the thinks always run in my mind. since we call sizeof as operator but y does we use it as sizeof(type) as a function... plese if any buddy finds it silly forgive me i even tried with google i didnt find it out..
hawk23reddy wrote:
y does we use it as sizeof(type) as a function...
Because that's what the C and C++ Standard's define. To quote the C++ Standard:
The sizeof operator yields the number of bytes in the object representation of its operand. The operand is either an expression, which is not evaluated, or a parenthesized typeid.
And you're better off using parentheses with expressions, as sizeof has higher precedence than most arithmetic operators. Consider this code:
#include <stdio.h>
int main(int argc, char** argv)
{
printf("%d\n", (int)(sizeof 3+4)); // Prints 8
printf("%d\n", (int)(sizeof (3+4))); // Prints 4
}Because sizeof has higher precedence than +, sizeof 3+4 == (sizeof 3) + 4.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
hi all, i am sorry i dont know weather i could ask this question ornot coz, these r the thinks always run in my mind. since we call sizeof as operator but y does we use it as sizeof(type) as a function... plese if any buddy finds it silly forgive me i even tried with google i didnt find it out..
The parenthesis are required if you are requesting the size of a type (e.g.,
int
). Otherwise, the parenthesis are optional."Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
well ... technically speaking ... every operator IS a function. So what's the difference ?
2 bugs found. > recompile ... 65534 bugs found. :doh:
emilio_grv wrote:
well ... technically speaking ... every operator IS a function
In a language like Haskell, yes. In C or C++, not so much - for example, can you show me how to get the address of +, so I can use it as a functor in an STL algorithm? To elaborate on the Haskell reference - any operator can be used where a function could be used by surrounding it in parentheses. Similarly, any function can be used as an operator by enclosing it in backticks. For example:
-- Define an addition function
add x y = x + y-- Define two functions that add integer lists. The first uses the + operator
-- as a function, the second uses add. They are equivalent.
addLists1 :: [Int] -> [Int] -> [Int]
addLists1 l1 l2 = zipWith (+) l1 l2addLists2 :: [Int] -> [Int] -> [Int]
addLists2 l1 l2 = zipWith add l1 l2-- The 4 lines of main are equivalent - they all add 3 and 4 with
-- different combination of operator/function and operator/function syntax.
main = do
print (add 3 4)
print ((+) 3 4)
print (3 `add` 4)
print (3 + 4)Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
The parenthesis are required if you are requesting the size of a type (e.g.,
int
). Otherwise, the parenthesis are optional."Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
hi thankx for answer but y for int (or types) the parenthesis is required is there any conceret reason
-
hi thankx for answer but y for int (or types) the parenthesis is required is there any conceret reason
As I said in this reply[^] - because the standard says so... But it's probably to simplify parsing - lord knows C++ (and to a degree, C) parsers need all the help they can get.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
emilio_grv wrote:
well ... technically speaking ... every operator IS a function
In a language like Haskell, yes. In C or C++, not so much - for example, can you show me how to get the address of +, so I can use it as a functor in an STL algorithm? To elaborate on the Haskell reference - any operator can be used where a function could be used by surrounding it in parentheses. Similarly, any function can be used as an operator by enclosing it in backticks. For example:
-- Define an addition function
add x y = x + y-- Define two functions that add integer lists. The first uses the + operator
-- as a function, the second uses add. They are equivalent.
addLists1 :: [Int] -> [Int] -> [Int]
addLists1 l1 l2 = zipWith (+) l1 l2addLists2 :: [Int] -> [Int] -> [Int]
addLists2 l1 l2 = zipWith add l1 l2-- The 4 lines of main are equivalent - they all add 3 and 4 with
-- different combination of operator/function and operator/function syntax.
main = do
print (add 3 4)
print ((+) 3 4)
print (3 `add` 4)
print (3 + 4)Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
Stuart Dootson wrote:
can you show me how to get the address of +, so I can use it as a functor in an STL algorithm?
Good point! And that's probably the difference that makes the standard deployers to use different therms. In theory it shold be
&operator+
, but that's ambiguous because of its overloading ... Thanks for pointing this out.2 bugs found. > recompile ... 65534 bugs found. :doh:
-
Stuart Dootson wrote:
can you show me how to get the address of +, so I can use it as a functor in an STL algorithm?
Good point! And that's probably the difference that makes the standard deployers to use different therms. In theory it shold be
&operator+
, but that's ambiguous because of its overloading ... Thanks for pointing this out.2 bugs found. > recompile ... 65534 bugs found. :doh:
Mmm - thinking about it, you *can* use something like that for class/struct types:
struct XX
{
};
XX operator+( XX const&, XX const& ) {}struct YY
{
};
YY operator+( YY const&, YY const& ) {}int main()
{
&static_cast<XX( & )( XX const&,XX const& )>(operator+);
&static_cast<YY( & )( YY const&,YY const& )>(operator+);
}The static_cast effectively specifies which operator+ you want. But you can't use that for built-in types.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
hi all, i am sorry i dont know weather i could ask this question ornot coz, these r the thinks always run in my mind. since we call sizeof as operator but y does we use it as sizeof(type) as a function... plese if any buddy finds it silly forgive me i even tried with google i didnt find it out..
sizeof
is not a function. It is an operator. Just because parenthesis is used does mean that it is a function. In that caseif
,switch
,while
,return
etc. can be called functions. This is a excerpt from MSDN - The operand to sizeof can be one of the following: A type name. To use sizeof with a type name, the name must be enclosed in parentheses. An expression. When used with an expression, sizeof can be specified with or without the parentheses. The expression is not evaluated.«_Superman_» I love work. It gives me something to do between weekends.