Welcome, Guest. Please login or register.

Author Topic: sizeof (*VarPtr) possible ?  (Read 5334 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show all replies
    • http://www.iki.fi/sintonen/
Re: sizeof (*VarPtr) possible ?
« on: January 30, 2007, 06:31:32 PM »
Quote
It seems that sizeof() works only with variable names themselves (or their types) but doesn't accept using a pointer to get the size of the variable pointed to.

Works fine here.

If you have pointer to pointer, and want to get size of it, then you obviously need to specify the type explicitly (or cast it, or if you know the type just use sizeof(type)).

Quote
No, I was expecting the compiler to assume (compile-time of course) that sizeof (*VarPtr) would be the same as sizeof (Var), simply because it's logic. This is assuming that Var is the variable VarPtr points to.

That is how it works.
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show all replies
    • http://www.iki.fi/sintonen/
Re: sizeof (*VarPtr) possible ?
« Reply #1 on: January 30, 2007, 08:41:52 PM »
@Karlos
Quote
sizeof() and offsetof() are usually implemented as macros on most implementations.

Wrong. 'sizeof' cannot be implemented with a macro. It's always builtin keyword in the compiler.

'offsetof' is a standard macro, in stddef.h (even standardized in POSIX these days).

Quote
That's the first indication that you can't absolutely rely on their behaviour in all cases.

Actually you can rely on sizeof.

Quote
Given that, I should point out that it can only work with concrete types that have already been "seen" by the compiler.

Well isn't that quite obvious, huh?

Quote
C++

C++ isn't C. ;-)
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show all replies
    • http://www.iki.fi/sintonen/
Re: sizeof (*VarPtr) possible ?
« Reply #2 on: January 30, 2007, 11:12:58 PM »
@Karlos

Nor does it handle sizeof variable.