473,786 Members | 2,407 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
16 1567
"ju**********@y ahoo.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**********@y ahoo.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.n et> writes:
On Mon, 26 Jun 2006 18:12:20 +0200, "Richard G. Riley"
<rg*****@gmail. com> wrote:
"ju********** @yahoo.co.in" <ju**********@y ahoo.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**********@ya hoo.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**********@ya hoo.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.embed ded or to some system-specific newsgroup.

--
Keith Thompson (The_Other_Keit h) 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*********@gm ail.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
1863
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 those allocations) which also loses some performance. I am specifically talking about many little allocations (approx. 16-512 bytes). Is my feeling about this justified? thanks! --
15
1475
by: Mona | last post by:
class myPoly { private: int nv; double *x, *y; int *p; public: myPoly() {
3
3940
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
2948
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 have a local identical variable name and want to save/load it to/from the global with same name * while you add code, the definition of globals moves more and more apart from their use cases -> weirdness; programmers thinking is fragmented * using...
19
3817
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 resources. So far i have tried to use #include <stdio.h> int main() { int *ip;
3
1915
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 a server. In my login function I do this: CAgents myagent;
20
2093
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 matrices, but as matrix dimension grows up it crashes in deterministic locations depending on data, machine and OS. In particular, this happens when I test it with 3000x3000 matrices (which take up more than 30MB) under three different machines/OSes....
5
3875
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 machine language, your application is given a certain amount of memory to use, that memory space is divided into 4 segments as follow a. Code segment, where all application code is stored b. Data segment, where global data is stored c. Stack segment,...
13
1999
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 have unfreeable (but still technically reachable) memory hanging around but it is, I think still a useful thing to do: void fn(int *stuff, int nstuffs) { static int *temp_stuff = NULL; static int ntemp_stuffs = 0;
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9492
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10163
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10108
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9960
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4064
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 we have to send another system
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.