473,771 Members | 2,328 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 1566
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 ".
Jun 26 '06 #2
ju**********@ya hoo.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**********@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.

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**********@y ahoo.co.in"
<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.

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.embed ded, 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**********@ya hoo.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.l earn.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*********@gm ail.com> wrote
in comp.lang.c:

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.


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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jun 26 '06 #8
"ju**********@y ahoo.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.

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

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

Similar topics

10
1862
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
1474
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
2947
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
1914
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
10260
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10038
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
8933
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...
1
7460
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6712
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
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...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.