allocating space for local variables

J

junky_fellow

Hi guys,

Consider the following piece of code:

func()
{

if(x==0) {
int arr[50];
/* do something */
}
else {
int arr2[100];
/* do something */
}
}


I wanted to know, if the space of arr[50 is allocated only when the
condition "x==0" is
true or the space is allocated when the function "func" is called.
I am working on an m68k implementation where the local varialbles are
allocated
on stack. I would be really grateful if anyone can provide me an
answer for an m68k
implementation.

thanks a lot for any help in advance ...
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

Hi guys,

Consider the following piece of code:

func()
{

if(x==0) {
int arr[50];
/* do something */
}
else {
int arr2[100];
/* do something */
}
}


I wanted to know, if the space of arr[50 is allocated only when the
condition "x==0" is
true or the space is allocated when the function "func" is called.
I am working on an m68k implementation where the local varialbles are
allocated
on stack. I would be really grateful if anyone can provide me an
answer for an m68k

The answer would be compiler dependant, compiler flags dependant, and
code dependant. Some code , depending on the optimizations/etc. might be
reordered to play things out very differently than just some very
slightly different code.

Your best bet is to peek at the assembly(if available - gcc -S for gcc)
generated for the exact code you have compiled with the options you'll
be using in "production".
 
F

Frederick Gotham

(e-mail address removed) posted:
Hi guys,

Consider the following piece of code:

func()
{

if(x==0) {
int arr[50];
/* do something */
}
else {
int arr2[100];
/* do something */
}
}


I wanted to know, if the space of arr[50 is allocated only when the
condition "x==0" is
true or the space is allocated when the function "func" is called.
I am working on an m68k implementation where the local varialbles are
allocated
on stack. I would be really grateful if anyone can provide me an
answer for an m68k
implementation.

thanks a lot for any help in advance ...



You could use an anonymous union. (Please correct me if they're not
Standard C).

union {
int arr[50];
int arr2[100];
};
 
D

deepak

Nils said:
Hi guys,

Consider the following piece of code:

func()
{

if(x==0) {
int arr[50];
/* do something */
}
else {
int arr2[100];
/* do something */
}
}


I wanted to know, if the space of arr[50 is allocated only when the
condition "x==0" is
true or the space is allocated when the function "func" is called.
I am working on an m68k implementation where the local varialbles are
allocated
on stack. I would be really grateful if anyone can provide me an
answer for an m68k

The answer would be compiler dependant, compiler flags dependant, and
code dependant. Some code , depending on the optimizations/etc. might be
reordered to play things out very differently than just some very
slightly different code.

Please correct me if i'm wrong.
Memory will be allocated for both arrays since it happens at
compilation time.
Here malloc comes into picture. It 'll do it at run time.

Once again i'm telling it's only my understanding. If wrong correct me.
 
A

Al Balmer

Hi guys,

Consider the following piece of code:

func()
{

if(x==0) {
int arr[50];
/* do something */
}
else {
int arr2[100];
/* do something */
}
}


I wanted to know, if the space of arr[50 is allocated only when the
condition "x==0" is
true or the space is allocated when the function "func" is called.
I am working on an m68k implementation where the local varialbles are
allocated
on stack. I would be really grateful if anyone can provide me an
answer for an m68k
implementation.
In the context of this newsgroup, you don't know and should not care
when the allocation takes place. You must write code as if it took
place at the beginning of the block. For your particular problem, I
suggest you post on comp.arch.embedded, and provide more detail about
the implementation you're using.
 
J

Jack Klein

(e-mail address removed) posted:
Hi guys,

Consider the following piece of code:

func()
{

if(x==0) {
int arr[50];
/* do something */
}
else {
int arr2[100];
/* do something */
}
}


I wanted to know, if the space of arr[50 is allocated only when the
condition "x==0" is
true or the space is allocated when the function "func" is called.
I am working on an m68k implementation where the local varialbles are
allocated
on stack. I would be really grateful if anyone can provide me an
answer for an m68k
implementation.

thanks a lot for any help in advance ...



You could use an anonymous union. (Please correct me if they're not
Standard C).

They are not.
union {
int arr[50];
int arr2[100];
};

But you could define the union with a name:

union
{
int arr [50];
int arr2 [100];
} u;
 
T

Tom St Denis

deepak said:
Please correct me if i'm wrong.
Memory will be allocated for both arrays since it happens at
compilation time.
Here malloc comes into picture. It 'll do it at run time.

Once again i'm telling it's only my understanding. If wrong correct me.

This is where it pays to actually earn your post-secondary degree...

No, first off there is no runtime call to malloc.

Second, the implementation is free to either allocate enough memory to
hold the largest of the two (note that you can't access both
simultaneously) or allocate it as required.

By allocate I mean "make available". That may be a call to malloc
(likely it won't be) or a call to alloca() or just subtract the stack
pointer or .... .... ... it's not predefined. Recent releases of GCC
[by that I mean 3.x series and up] will analyze the function and
allocate stack in the preamble. But that is just how GCC chooses to do
it.

Tom
 
J

Jack Klein

Hi guys,

Consider the following piece of code:

func()
{

if(x==0) {
int arr[50];
/* do something */
}
else {
int arr2[100];
/* do something */
}
}


I wanted to know, if the space of arr[50 is allocated only when the
condition "x==0" is
true or the space is allocated when the function "func" is called.
I am working on an m68k implementation where the local varialbles are
allocated
on stack. I would be really grateful if anyone can provide me an
answer for an m68k

The answer would be compiler dependant, compiler flags dependant, and
code dependant. Some code , depending on the optimizations/etc. might be
reordered to play things out very differently than just some very
slightly different code.

Please correct me if i'm wrong.

Consider yourself corrected.
Memory will be allocated for both arrays since it happens at
compilation time.

That is true for objects with static storage duration, specifically
all objects defined at file scope or with the 'static' keyword.
Automatic objects do not need to be allocated until the block
containing their definition is entered.
 
R

Richard G. Riley

Hi guys,

Consider the following piece of code:

func()
{

if(x==0) {
int arr[50];
/* do something */
}
else {
int arr2[100];
/* do something */
}
}


I wanted to know, if the space of arr[50 is allocated only when the
condition "x==0" is
true or the space is allocated when the function "func" is called.
I am working on an m68k implementation where the local varialbles are
allocated
on stack. I would be really grateful if anyone can provide me an
answer for an m68k
implementation.

I'm interested in to why you would need to determine the difference even
assuming non portable code and the assertion that all locals are stack
based.
 
K

Kenneth Brody

Hi guys,

Consider the following piece of code:

func()
{

if(x==0) {
int arr[50];
/* do something */
}
else {
int arr2[100];
/* do something */
}
}

I wanted to know, if the space of arr[50 is allocated only when the
condition "x==0" is
true or the space is allocated when the function "func" is called.
I am working on an m68k implementation where the local varialbles are
allocated
on stack. I would be really grateful if anyone can provide me an
answer for an m68k
implementation.

thanks a lot for any help in advance ...

Well, the only way to know how it's done on your implementation would
be to examine the code that is generated by your compiler. However,
as has been pointed out, it's possible for this to change if you use
different flags to the compiler, let alone use a different compiler
altogether.

I've seen compilers that adjust the local stack frame when entering
the relevent section of code, allocating the local variables in that
stack space, and re-adjusting the stack frame upon leaving the
section. I've seen others that allocate enough stack space based on
the largest amount that would be needed by any particular section of
code, and then do the equivalent of a union on the stack, with each
section of code using the same stack memory for their own local
variables.

Finally, as others have pointed out, why should it matter?

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
R

robertwessel2

Tom said:
This is where it pays to actually earn your post-secondary degree...

No, first off there is no runtime call to malloc.

Second, the implementation is free to either allocate enough memory to
hold the largest of the two (note that you can't access both
simultaneously) or allocate it as required.


The implementation is also free to allocate both areas on the stack (if
such a thing is being used) at entry to the routine, or one on the
stack and the other dynamically, or...

Commonly these will be allocated on the stack as if they had been in a
union.
 
A

Al Balmer

Hi guys,

Consider the following piece of code:

func()
{

if(x==0) {
int arr[50];
/* do something */
}
else {
int arr2[100];
/* do something */
}
}


I wanted to know, if the space of arr[50 is allocated only when the
condition "x==0" is
true or the space is allocated when the function "func" is called.
I am working on an m68k implementation where the local varialbles are
allocated
on stack. I would be really grateful if anyone can provide me an
answer for an m68k
implementation.

I'm interested in to why you would need to determine the difference even
assuming non portable code and the assertion that all locals are stack
based.

It cant be a memory thing since I assume x<>0 at some stage anyway so
you need to assume the stack space required for at leas 100 integers.

The OP is probably working on an embedded system with memory
constraints. It may be important to know whether he needs to assume
the space required for 100 integers or 150 integers.
 
R

Richard G. Riley

Al Balmer said:
Hi guys,

Consider the following piece of code:

func()
{

if(x==0) {
int arr[50];
/* do something */
}
else {
int arr2[100];
/* do something */
}
}


I wanted to know, if the space of arr[50 is allocated only when the
condition "x==0" is
true or the space is allocated when the function "func" is called.
I am working on an m68k implementation where the local varialbles are
allocated
on stack. I would be really grateful if anyone can provide me an
answer for an m68k
implementation.

I'm interested in to why you would need to determine the difference even
assuming non portable code and the assertion that all locals are stack
based.

It cant be a memory thing since I assume x<>0 at some stage anyway so
you need to assume the stack space required for at leas 100 integers.

The OP is probably working on an embedded system with memory
constraints. It may be important to know whether he needs to assume
the space required for 100 integers or 150 integers.

Both of which are possible so you need to plan for worst case I would
have thought.
 
A

Al Balmer

Both of which are possible so you need to plan for worst case I would
have thought.

Nope, not when doing embedded systems. You need to determine what the
actual worst case is for your hardware and language implementation,
not the worst possible case for any implementation.

That's why this is off-topic here, and the OP will get better advice
in another newsgroup.
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

deepak said:
Nils said:
Hi guys,

Consider the following piece of code:

func()
{

if(x==0) {
int arr[50];
/* do something */
}
else {
int arr2[100];
/* do something */
}
}


I wanted to know, if the space of arr[50 is allocated only when the
condition "x==0" is
true or the space is allocated when the function "func" is called.
I am working on an m68k implementation where the local varialbles are
allocated
on stack. I would be really grateful if anyone can provide me an
answer for an m68k
The answer would be compiler dependant, compiler flags dependant, and
code dependant. Some code , depending on the optimizations/etc. might be
reordered to play things out very differently than just some very
slightly different code.

Please correct me if i'm wrong.
Memory will be allocated for both arrays since it happens at
compilation time.
Here malloc comes into picture. It 'll do it at run time.

The implementation(compiler...) is free to do whatever it wants
as long as the meaning doesn't change. It's not uncommon for
an optimizing C compiler to turn your C code upside down and
whatnot comparing it to the assembly/machinecode it might emit.
(e.g. you might assume
void freelist(Node *n) {
if(n != NULL) {
Node *next = n->next;
free(n);
freelist(next);
}
}
takes space proportional to the length of the list as it recurses down.
I was happy to see the C compiler I use effectivly turn the above to a
goto loop involving no recursive calls - aka tail call optimization :)
 
K

Keith Thompson

Nils O. Selåsdal said:
Hi guys,
Consider the following piece of code:
func()
{
if(x==0) {
int arr[50];
/* do something */
}
else {
int arr2[100];
/* do something */
}
}
I wanted to know, if the space of arr[50 is allocated only when the
condition "x==0" is
true or the space is allocated when the function "func" is called.
I am working on an m68k implementation where the local varialbles are
allocated
on stack. I would be really grateful if anyone can provide me an
answer for an m68k

The answer would be compiler dependant, compiler flags dependant, and
code dependant. Some code , depending on the optimizations/etc. might be
reordered to play things out very differently than just some very
slightly different code.

Your best bet is to peek at the assembly(if available - gcc -S for gcc)
generated for the exact code you have compiled with the options you'll
be using in "production".

Another approach would be to examine the addresses of the array
objects, either using printf with a "%p" format (and the required cast
to void*) or a debugger. With some reasonable assumptions about how
memory is allocated, this should tell you what you need to know.

But of course the results will be specific to whatever compiler you're
using, and possibly to the options with which you invoke it.

The answer is unlikely to depend on the fact that you're using an
m68k. Different compilers for the same CPU are free to do this
differently; so is the same compiler with different options.

If your compiler allocates 150 bytes for arr and arr2 (with
appropriate optimization options), consider complaining to your
vendor. It's not a violation of the standard, but it's a waste of
space since arr and arr2 cannot exist simultaneously.

A compiler could reasonable allocate max(sizeof arr, sizeof arr2) on
entry to the function, or allocate just enough for a single array on
entry to the block contaiing its declaration. The latter saves space
in some cases at the expense of some extra code.

If you're desparate for space *and* your compiler doesn't do the
optimization you need, you can combine the declarations yourself,
being careful to guarantee that you always allocate enough space for
the object you need. The simplest way to do this would be to put arr
and arr2 in separate functions, though that imposes some additional
overhead as well.

Discussion more detailed than this should probably go to
comp.arch.embedded or to some system-specific newsgroup.
 
M

Mark McIntyre

Please correct me if i'm wrong.
Memory will be allocated for both arrays since it happens at
compilation time.

As someone said earlier, its compiler and optimiser dependent.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top