Re: Discovering variable types...

From: Rob Kennedy (me3_at_privacy.net)
Date: 08/08/04


Date: Sun, 08 Aug 2004 14:29:57 -0500

not@any.adr wrote:
> The hitch is that Delphi doesn't store static variables (the ones we declare
> with Var statements on the heap. They're stored in a block at the top of the
> EXE file itself so that when you load the program you're actually loading the
> memory allocations for your variables from disk as well. If you notice the
> Delphi $M compiler directive lacks the memory allocation variable and now only
> does stack size. This is why.

If I were writing a compiler, I probably wouldn't allocate the global
variables on the heap, either. Why allocate them specially when you can
have the OS loader do it for you automatically? Even if I did allocate
them dynamically during startup, I certainly wouldn't allocate each
variable separately. I'd allocate one block large enough to hold them
all and then access them via offsets from the block's base address.

> The heapsize function only works for dynamic variables (from getmem or new)
> it's going to return an error with pointers to anything else. Sizeof only
> works with static variables and is going to return the size of the pointer (4)
> for dynamics.

SizeOf works on all variables. It returns the size of the variable. It
does not ever return the size of the memory allocated at the address
stored in that variable. SizeOf never inspects a variable's value.

> Interestingly SizeOf can delve into this memory area and get back the sizes of
> static variables by name. Looking at the code linked in by a sizeof call it
> appears to access some kind of table... but I haven't investigated it very far
> as yet.

SizeOf is a compile-time constant. It generates no code at all. The only
memory it delves into is the compiler's internal symbol table, which
doesn't exist at run time. That's how it can check the size based on the
name -- the compiler knows the name and the compiler evaluates SizeOf
right then and there.

-- 
Rob


Relevant Pages

  • Re: Mex Overflow Error Using Free Borland Compiler
    ... >>Borland compiler handles overflow errors. ... >>The first thing I tried is dynamic memory allocation. ... 1- You declare the variables of type double and allocate of size long double. ... would clean up a lot of code by eliminating the pointer dereferences. ...
    (comp.soft-sys.matlab)
  • Re: variable allocated from stack/bss ??
    ... "BSS" is one of these things. ... the phrase "uninitialized global or static variables" is ... initialier. ... Some implementations may allocate such variables from a ...
    (comp.lang.c)
  • Re: Many small programs vs. one large program?
    ... Allocate ) ... compiler supports but which might be unique to that compiler. ... You can, if you insist, still use tabs for indention, ... Why would anyone want to use a fixed form source code? ...
    (comp.lang.fortran)
  • Re: The Philosophy of Programming?
    ... A compiler is free to ... >>is to allocate on entry to almost any block. ... > int foo{ ... But any programmer who wants to be more than a coder prefers, I think, ...
    (comp.programming)
  • Re: Mex Overflow Error Using Free Borland Compiler
    ... >>>Borland compiler handles overflow errors. ... >>>The first thing I tried is dynamic memory allocation. ... > 1- You declare the variables of type double and allocate of size long double. ... > would clean up a lot of code by eliminating the pointer dereferences. ...
    (comp.soft-sys.matlab)