Locals vars inside a static member function

R

RishiD

Hi,

I am trying to figure out if the local variables created within a
static member function are created on the heap or stack?

Example:

class Circle
{



};
 
R

RishiD

Sorry Hit send early. Trying to figure out where "int rc" inside
getArea() would be stored.

class Circle
{
public:
static int getArea()
{
int rc = Radi * Radi * 3; // where is this stored... on the
heap or stack?
return rc;
}

private:
static int Radi;
}
 
R

RishiD

No I understand I dont have to do that. I am working on an embedded
processor so I am trying to figure it out because we are limited on
the stack size.
 
J

Joe Greer

No I understand I dont have to do that. I am working on an embedded
processor so I am trying to figure it out because we are limited on
the stack size.

In the specific case shown, I would be surprised if any space were
allocated for rc. The compiler can see that you just return it and so may
well not even allocate any space on the stack (or anywhere else) for it.
As a worst case situation, it will generally go on the stack. The compiler
is really in control of what it actually does though. This is one of those
cases where I wish that C++ allowed you to directly manipulate the return
value (like Pascal and a few other languages) so you didn't have to rely on
the optimizer figuring out what you were doing.

joe
 
B

Bo Persson

Joe said:
In the specific case shown, I would be surprised if any space were
allocated for rc. The compiler can see that you just return it and
so may well not even allocate any space on the stack (or anywhere
else) for it. As a worst case situation, it will generally go on
the stack. The compiler is really in control of what it actually
does though. This is one of those cases where I wish that C++
allowed you to directly manipulate the return value (like Pascal
and a few other languages) so you didn't have to rely on the
optimizer figuring out what you were doing.

But wouldn't that instead make us have to outsmart the compiler
writers?


If it cannot fugure out how to compute and return an integer, what
else can it do?

Trust your compiler!


Bo Persson
 
J

Joe Greer

Bo Persson said:
But wouldn't that instead make us have to outsmart the compiler
writers?


If it cannot fugure out how to compute and return an integer, what
else can it do?

Trust your compiler!

I would love to be able to. Sadly, compilers have let me down too many
times.

joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top