How to detect if an array if dynamic or static

Chris Wright via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Feb 24 17:31:17 PST 2016


On Wed, 24 Feb 2016 21:48:14 +0000, mahdi wrote:

> Suppose we have a function like this:
> 
> void diss(int[] array) ...
> 
> How can we detect is `array` is static (fixed size) or dynamic,
> inside the function body?

Static arrays point to memory on the stack, inside an aggregate type on 
the heap, or inside the static data area. In the stack case, you can use 
this strategy:

extern (C) void* thread_stackBottom();

bool isLocatedOnStack(T)(T[] arr) {
  int i;
  void* top = &i;
  auto bottom = thread_stackBottom();
  return arr.ptr >= min(top, bottom) && arr.ptr <= max(top, bottom);
}

When you get to GC-allocated stuff, there's no way to tell. Static data, 
I'm not sure.

This probably isn't what you want, though.


More information about the Digitalmars-d-learn mailing list