Variable on stack or heap?

View: New views
9 Messages — Rating Filter:   Alert me  

Variable on stack or heap?

by Gavin Rehkemper :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi everyone,

We are inspecting the AST in CDT. Is there any way to figure out if a particular variable was declared on the stack or on the heap? Any help on this topic would be appreciated.

Thanks,
Gavin

_______________________________________________
cdt-dev mailing list
cdt-dev@...
https://dev.eclipse.org/mailman/listinfo/cdt-dev

Re: Variable on stack or heap?

by Alena Laskavaia :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Never herd of syntax to declare variable in C in the heap. What do you mean?

On Thu, Nov 5, 2009 at 7:18 PM, Gavin Rehkemper <gavreh@...> wrote:

> Hi everyone,
>
> We are inspecting the AST in CDT. Is there any way to figure out if a
> particular variable was declared on the stack or on the heap? Any help on
> this topic would be appreciated.
>
> Thanks,
> Gavin
>
> _______________________________________________
> cdt-dev mailing list
> cdt-dev@...
> https://dev.eclipse.org/mailman/listinfo/cdt-dev
>
>
_______________________________________________
cdt-dev mailing list
cdt-dev@...
https://dev.eclipse.org/mailman/listinfo/cdt-dev

Parent Message unknown Re: Variable on stack or heap?

by Gavin Rehkemper :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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).

Thanks for the help!
Gavin
On Fri, Nov 6, 2009 at 6:51 AM, <cdt-dev-request@...> wrote:
Date: Thu, 5 Nov 2009 20:09:28 -0500
From: Alena Laskavaia <elaskavaia.cdt@...>
Subject: Re: [cdt-dev] Variable on stack or heap?
To: "CDT General developers list." <cdt-dev@...>
Message-ID:
       <5498d4500911051709k290c4e5eocaf19a8f50c155ce@...>
Content-Type: text/plain; charset=ISO-8859-1
Never herd of syntax to declare variable in C in the heap. What do you mean?
On Thu, Nov 5, 2009 at 7:18 PM, Gavin Rehkemper <gavreh@...> wrote:
> Hi everyone,
>
> We are inspecting the AST in CDT. Is there any way to figure out if a
> particular variable was declared on the stack or on the heap? Any help on
> this topic would be appreciated.
>
> Thanks,
> Gavin

_______________________________________________
cdt-dev mailing list
cdt-dev@...
https://dev.eclipse.org/mailman/listinfo/cdt-dev

Re: Variable on stack or heap?

by Pete Vidler :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@...
+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
_______________________________________________
cdt-dev mailing list
cdt-dev@...
https://dev.eclipse.org/mailman/listinfo/cdt-dev

Re: Variable on stack or heap?

by Tobias Hahn-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

You cannot.

int x = 0;
int* py = new int;

int* pInt = &x;
pInt = py;

Am 06.11.2009 um 16:42 schrieb Gavin Rehkemper:

> 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).
>
> Thanks for the help!
> Gavin
> On Fri, Nov 6, 2009 at 6:51 AM, <cdt-dev-request@...> wrote:
> Date: Thu, 5 Nov 2009 20:09:28 -0500
> From: Alena Laskavaia <elaskavaia.cdt@...>
> Subject: Re: [cdt-dev] Variable on stack or heap?
> To: "CDT General developers list." <cdt-dev@...>
> Message-ID:
>        <5498d4500911051709k290c4e5eocaf19a8f50c155ce@...>
> Content-Type: text/plain; charset=ISO-8859-1
> Never herd of syntax to declare variable in C in the heap. What do  
> you mean?
> On Thu, Nov 5, 2009 at 7:18 PM, Gavin Rehkemper <gavreh@...>  
> wrote:
> > Hi everyone,
> >
> > We are inspecting the AST in CDT. Is there any way to figure out  
> if a
> > particular variable was declared on the stack or on the heap? Any  
> help on
> > this topic would be appreciated.
> >
> > Thanks,
> > Gavin
> _______________________________________________
> cdt-dev mailing list
> cdt-dev@...
> https://dev.eclipse.org/mailman/listinfo/cdt-dev

Ableton AG, Sitz Berlin, Amtsgericht Berlin-Charlottenburg, HRB 72838
Vorstand: Gerhard Behles, Jan Bohl, Bernd Roggendorf
Vorsitzender des Aufsichtsrats: Uwe Struck




_______________________________________________
cdt-dev mailing list
cdt-dev@...
https://dev.eclipse.org/mailman/listinfo/cdt-dev

Parent Message unknown Re: Variable on stack or heap?

by Gavin Rehkemper :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@...> 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@...
+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

_______________________________________________
cdt-dev mailing list
cdt-dev@...
https://dev.eclipse.org/mailman/listinfo/cdt-dev

Re: Variable on stack or heap?

by Pete Vidler :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 7 Nov 2009, at 17:09, Gavin Rehkemper wrote:
> 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?

How would you know which comes last without running it?  An AST is for  
one translation unit only remember - a global (for example) could also  
be changed elsewhere.  Statically tracing the execution path of a non-
trivial program is virtually impossible.

> 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.

You can tell the difference between a pointer and an array statically,  
but not if it's been passed to a function or cast to something else.  
After all, a function taking a pointer could be called with both  
malloc'ed and array-based memory in the same program...

Pete

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

p.vidler@...
+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

_______________________________________________
cdt-dev mailing list
cdt-dev@...
https://dev.eclipse.org/mailman/listinfo/cdt-dev

Re: Variable on stack or heap?

by Alena Laskavaia :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

This is classic static analysis problem and can be partially solved by
data-flow analysis, but never can be solved completely,
i.e. but doing data-flow you can figure out if variable at given line
contains pointer to heap, pointer to non-heap, either or unknown.
Depends on depth and complexity
you reduce "unknown" factor. You cannot do it from AST you need to
build control-flow graph, data-flow graph and perform data-flow
evaluation for particular know "seeds"
i.e. call to malloc or new is known to return value in heap, then you
propagate this value through the graph.

On Sat, Nov 7, 2009 at 12:09 PM, Gavin Rehkemper <gavreh@...> wrote:

> 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@...> 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@...
> +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
>
> _______________________________________________
> cdt-dev mailing list
> cdt-dev@...
> https://dev.eclipse.org/mailman/listinfo/cdt-dev
>
>
_______________________________________________
cdt-dev mailing list
cdt-dev@...
https://dev.eclipse.org/mailman/listinfo/cdt-dev

Re: Variable on stack or heap?

by Tobias Hahn-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks Alena, I couldn't have phrased it better :)

Am 08.11.2009 um 02:06 schrieb Alena Laskavaia:

> This is classic static analysis problem and can be partially solved by
> data-flow analysis, but never can be solved completely,
> i.e. but doing data-flow you can figure out if variable at given line
> contains pointer to heap, pointer to non-heap, either or unknown.
> Depends on depth and complexity
> you reduce "unknown" factor. You cannot do it from AST you need to
> build control-flow graph, data-flow graph and perform data-flow
> evaluation for particular know "seeds"
> i.e. call to malloc or new is known to return value in heap, then you
> propagate this value through the graph.
>
> On Sat, Nov 7, 2009 at 12:09 PM, Gavin Rehkemper <gavreh@...>  
> wrote:
>> 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@...> 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@...
>> +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
>>
>> _______________________________________________
>> cdt-dev mailing list
>> cdt-dev@...
>> https://dev.eclipse.org/mailman/listinfo/cdt-dev
>>
>>
> _______________________________________________
> cdt-dev mailing list
> cdt-dev@...
> https://dev.eclipse.org/mailman/listinfo/cdt-dev

_______________________________________________
cdt-dev mailing list
cdt-dev@...
https://dev.eclipse.org/mailman/listinfo/cdt-dev