473,508 Members | 2,267 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

allocating space for local variables

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

Jun 26 '06 #1
16 1536
ju**********@yahoo.co.in wrote:
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".
Jun 26 '06 #2
ju**********@yahoo.co.in 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];
};
--

Frederick Gotham
Jun 26 '06 #3

Nils O. Selåsdal wrote:
ju**********@yahoo.co.in wrote:
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.
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".


Jun 26 '06 #4
On 26 Jun 2006 01:38:56 -0700, "ju**********@yahoo.co.in"
<ju**********@yahoo.co.in> wrote:
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.

--
Al Balmer
Sun City, AZ
Jun 26 '06 #5
On Mon, 26 Jun 2006 13:36:56 GMT, Frederick Gotham
<fg*******@SPAM.com> wrote in comp.lang.c:
ju**********@yahoo.co.in 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;

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jun 26 '06 #6
deepak wrote:
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

Jun 26 '06 #7
On 26 Jun 2006 07:48:31 -0700, "deepak" <de*********@gmail.com> wrote
in comp.lang.c:

Nils O. Selåsdal wrote:
ju**********@yahoo.co.in wrote:
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.
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.
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".


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jun 26 '06 #8
"ju**********@yahoo.co.in" <ju**********@yahoo.co.in> writes:
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.

thanks a lot for any help in advance ...


--
Lint early. Lint often.
Jun 26 '06 #9

Tom St Denis wrote:
deepak wrote:
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.

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.

Jun 26 '06 #10
"ju**********@yahoo.co.in" wrote:

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:Th*************@gmail.com>

Jun 26 '06 #11
On Mon, 26 Jun 2006 18:12:20 +0200, "Richard G. Riley"
<rg*****@gmail.com> wrote:
"ju**********@yahoo.co.in" <ju**********@yahoo.co.in> writes:
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.

thanks a lot for any help in advance ...


--
Al Balmer
Sun City, AZ
Jun 26 '06 #12
Al Balmer <al******@att.net> writes:
On Mon, 26 Jun 2006 18:12:20 +0200, "Richard G. Riley"
<rg*****@gmail.com> wrote:
"ju**********@yahoo.co.in" <ju**********@yahoo.co.in> writes:
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.
Jun 26 '06 #13
On Mon, 26 Jun 2006 19:42:30 +0200, "Richard G. Riley"
<rg*****@gmail.com> wrote:
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.


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.

--
Al Balmer
Sun City, AZ
Jun 26 '06 #14
deepak wrote:
Nils O. Selåsdal wrote:
ju**********@yahoo.co.in wrote:
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 :-)
Jun 26 '06 #15
"Nils O. Selåsdal" <NO*@Utel.no> writes:
ju**********@yahoo.co.in wrote:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 26 '06 #16
On 26 Jun 2006 07:48:31 -0700, in comp.lang.c , "deepak"
<de*********@gmail.com> wrote:
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
Jun 26 '06 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
1839
by: Jakob Bieling | last post by:
Hi, Whenever allocating memory using operator new or operator new I feel like I should only use it very sparingly, because otherwise memory is wasted (by additional overhead needed to manage all...
15
1450
by: Mona | last post by:
class myPoly { private: int nv; double *x, *y; int *p; public: myPoly() {
3
3899
by: Paminu | last post by:
If I have this struct: typedef struct test{ int x; int y; }container; Now I would like to make an array of 5 pointers to this struct: int main(void){
18
2927
by: robert | last post by:
Using global variables in Python often raises chaos. Other languages use a clear prefix for globals. * you forget to declare a global * or you declare a global too much or in conflict * you...
19
3786
by: allanallansson | last post by:
Hi i would like some guidelines for a experiment of mine. I want to experiment with the swap and ctrl-z in linux. And for this i want to create a c program that allocates almost all the free memory...
3
1897
by: Angus | last post by:
Hello I have a member variable: std::map<int, CAgentsm_AgentsList; CAgents is just a really small class with some member variables. Just really a container for agent data. Agents log in to...
20
2061
by: Neclepsio | last post by:
Hi everyone. I've made a class Matrix, which contains a pointer to the data and some methods which all return a copy of the matrix modified in some way. The program works quite well for small...
5
3863
by: vnpatriot7 | last post by:
Hi everybody, I have two questions: 1) About memory space in C++ 2) About global and static variable As what I read somewhere that after compilation process which translate C++ code into...
13
1966
by: charlie | last post by:
I came up with this idiom for cases where a function needs a variable amount of memory for it's temporary storage so as to avoid constantly mallocing. It makes me feel a little uncomfortable to...
0
7120
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7380
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5050
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1553
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.