Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [cdt-dev] Variable on stack or heap?

Thanks for all the help everyone.

Tobias, we're a bit confused by your example. x is on the stack, and *py is on the heap. In the first case, pint points to something on the stack, and the second, it points to something on the heap. So you're saying that because of this particular case we can never know if pInt is on the stack or heap? Is there a way to check the last assignment?

Pete, we are writing an eclipse plugin that is doing some intelligent code refactoring. To take an example, say we're trying to get the size of an array. We could either use sizeof() or malloc_usable_size() - and we want to figure out which one to use given a particular variable.

We were hoping there would be some aspect of the AST that would indicate this without having to run the code. So it sounds like you're saying there's no easy way to do this before runtime?

Thanks,
Gavin

On Fri, Nov 6, 2009 at 9:50 AM, <cdt-dev-request@xxxxxxxxxxx> wrote:
Gavin Rehkemper wrote:
> We have a variable, and we know it is a pointer. How do we determine
> whether it points to something allocated on the heap (malloc'd, for
> example), or something allocated on the stack (an array of finite length).

In the general case this is not possible from an AST, as it must be
determined at run-time (pointers can be changed to point at other
things, for example).

If the specific scenario is *very* simple, you could look for all the
assignments in the AST and *may* be able to determine from that exactly
what it is pointing to after the assignment completes.  Probably not
very likely or useful though.

At run-time you might be able to do this by checking the address that it
points to, assuming you know where the stack and heap are in memory.

What are you actually trying to achieve, if you don't mind me asking?

Pete

--
Peter J. Vidler
Senior Systems Developer, TTE Systems Ltd

p.vidler@xxxxxxxxxxxxxxx
+44 (0)116 223 1684

Registered address: 106 New Walk, Leicester, LE1 7EA
Company number: 05058157
VAT registration: 913 4859 12

www.tte-systems.com

Back to the top