TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: JAMIE KOWINSKY
from: GEORGE WHITE
date: 1997-11-01 11:04:00
subject: sizeof() question

Hi Jamie,
You asked:
JK>    I normally use sizeof on variables, but recently foudn
JK>out that it can be used on a struct name without even
JK>having a variable declared as that struct type.  for
JK>example if I have a struct called StudentType and I wanted
JK>to know the size of StudentType (without calc'ing by hand)
JK>I would do somthing like this:
JK>long studentsize;
JK>StudentType temp;
JK>studentsize = sizeof(temp);
JK>however i recently found out that I can simply do studentsize =
JK>sizeof(StudentType);  Well at least in Turbo C++ 4.5 one
JK>can, I don't know if this is standard.
JK>Now my question is, does the compiler treat
JK>"sizeof(StructName);" as a constant?  During compile time
JK>will it lookup the size of the stuct and simply use that
JK>number instead of calculating the size of the stucture each
JK>time?
As sizeof() is a compile time operator (ie it is evaluated during
compilation, not during program execution) there is no easy way of
knowing how the compiler evaluates it internally. It'll only affect
compile time anyway.
JK>I'm useing Turbo C++ 4.5, and with its limitations I'm
JK>guessing no, but I'm also wondeing if a 'good' c++ compiler
JK>would do it (ie Watcomm, Borland)...
I assume they will all behave similarly as the compiler has to know
the size of the structure internally when allocating space for
instances of the struct.
George
 * SLMR 2.1a * A mainframe: The biggest PC peripheral available.
--- Maximus/2 3.01
---------------
* Origin: DoNoR/2,Woking UK (44-1483-717905) (2:440/4)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.