473,624 Members | 2,584 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

static allocation question...

-- I didn't notice anything about this specifically
-- in the c.l.c. FAQ, nor could i get a strait answere
-- from my copy of the C standard. Hopeing someone
-- here could shed some light on it :)...

6.2.1
"If the declarator or type specifier that declares
the identifier appears inside a block or within the
list of parameter declarations in a function definition,
the identifier has block scope, which terminates at the
end of the associated block."

I believe this talks about what we learned early on :

int main(void)
{
{
int a;
}
printf("%d\n", a); /* ERROR - Invalid */
return 0;
}

Once outside of the closing } a is no longer accessible.
It's out of our current "scope". Which brings me to my
question. Since that data is static, is it automatically
allocated at program start up?

int main(void)
{
int i;
return 0;
}

As "i" would be in this example ? What if there's a conditional
statement there ? As in :

int main(void)
{
int i = 0;
if(!i) {
int a;
}
return 0;
}

If "a" is /NOT/ automatically allocated with "i", what is
the compiler doing to make sure it gets allocated only when,
and if it's ever needed (like if "i" changes before the
"if", or if it waits for input from a user which we'll assume
is unknown untill runtime will "a" still get allocated ?)

Any help is greatly appreciated, just something i started
wondering about :) FMorales...
Nov 13 '05 #1
34 2153
Did you try this?
In C you have to declare all variables first and then start your code.
So you would have to do
int a;
int i;

What you did is allowed in c++. I mean this code:
int main(void)
{
int i = 0;
if(!i) {
int a;
}
return 0;
}
Won't compile.

FMorales wrote: -- I didn't notice anything about this specifically
-- in the c.l.c. FAQ, nor could i get a strait answere
-- from my copy of the C standard. Hopeing someone
-- here could shed some light on it :)...

6.2.1
"If the declarator or type specifier that declares
the identifier appears inside a block or within the
list of parameter declarations in a function definition,
the identifier has block scope, which terminates at the
end of the associated block."

I believe this talks about what we learned early on :

int main(void)
{
{
int a;
}
printf("%d\n", a); /* ERROR - Invalid */
return 0;
}

Once outside of the closing } a is no longer accessible.
It's out of our current "scope". Which brings me to my
question. Since that data is static, is it automatically
allocated at program start up?

int main(void)
{
int i;
return 0;
}

As "i" would be in this example ? What if there's a conditional
statement there ? As in :

int main(void)
{
int i = 0;
if(!i) {
int a;
}
return 0;
}

If "a" is /NOT/ automatically allocated with "i", what is
the compiler doing to make sure it gets allocated only when,
and if it's ever needed (like if "i" changes before the
"if", or if it waits for input from a user which we'll assume
is unknown untill runtime will "a" still get allocated ?)

Any help is greatly appreciated, just something i started
wondering about :) FMorales...


Nov 13 '05 #2
On Sat, 04 Oct 2003 18:09:53 -0500, Pushkar Pradhan wrote:
Did you try this?
In C you have to declare all variables first and then start your code.
only in c89
in c99, variables can be declared anywhere.
So you would have to do
int a;
int i;

What you did is allowed in c++. I mean this code:
> int main(void)
> {
> int i = 0;
> if(!i) {
> int a;
> }
> return 0;
> }


Won't compile.

FMorales wrote:
-- I didn't notice anything about this specifically
-- in the c.l.c. FAQ, nor could i get a strait answere
-- from my copy of the C standard. Hopeing someone
-- here could shed some light on it :)...

6.2.1
"If the declarator or type specifier that declares
the identifier appears inside a block or within the
list of parameter declarations in a function definition,
the identifier has block scope, which terminates at the
end of the associated block."

I believe this talks about what we learned early on :

int main(void)
{
{
int a;
}
printf("%d\n", a); /* ERROR - Invalid */
return 0;
}

Once outside of the closing } a is no longer accessible.
It's out of our current "scope". Which brings me to my
question. Since that data is static, is it automatically
allocated at program start up?

int main(void)
{
int i;
return 0;
}

As "i" would be in this example ? What if there's a conditional
statement there ? As in :

int main(void)
{
int i = 0;
if(!i) {
int a;
}
return 0;
}
any good compiler won't generate code for your conditional statement
unless you set i to be "volatile".

If "a" is /NOT/ automatically allocated with "i", what is
the compiler doing to make sure it gets allocated only when,
and if it's ever needed (like if "i" changes before the
the compiler can't assume that unless you provide the keyword static
"if", or if it waits for input from a user which we'll assume
is unknown untill runtime will "a" still get allocated ?)

Any help is greatly appreciated, just something i started
wondering about :) FMorales...


Nov 13 '05 #3
Pushkar Pradhan wrote:
Did you try this?
In C you have to declare all variables first and then start your code.
So you would have to do
int a;
int i;

What you did is allowed in c++. I mean this code:
> int main(void)
> {
> int i = 0;
> if(!i) {
> int a;
> }
> return 0;
> }

Pushkar, you must have suffered a temporary access of madness. There is
nothing wrong with declaring variables at the beginning of a block. The
code above is perfectly valid C89 (as well as C99, of course).

--
Bertrand Mollinier Toublet
closity = 1.0 / farthitude
-- Arthur J. O'Dwyer

Nov 13 '05 #4
FMorales wrote:
-- I didn't notice anything about this specifically
-- in the c.l.c. FAQ, nor could i get a strait answere
-- from my copy of the C standard. Hopeing someone
-- here could shed some light on it :)...

6.2.1
"If the declarator or type specifier that declares
the identifier appears inside a block or within the
list of parameter declarations in a function definition,
the identifier has block scope, which terminates at the
end of the associated block."

I believe this talks about what we learned early on :

int main(void)
{
{
int a;
}
printf("%d\n", a); /* ERROR - Invalid */
return 0;
}

Once outside of the closing } a is no longer accessible.
It's out of our current "scope". Which brings me to my
Correct.
question. Since that data is static, is it automatically
allocated at program start up?
It's *not* static. It is an *automatic variable*.

int main(void)
{
int i;
return 0;
}

As "i" would be in this example ? What if there's a conditional
statement there ? As in :

int main(void)
{
int i = 0;
if(!i) {
int a;
}
`a' is now out of scope.
return 0;
}

If "a" is /NOT/ automatically allocated with "i", what is
the compiler doing to make sure it gets allocated only when,
and if it's ever needed (like if "i" changes before the
"if", or if it waits for input from a user which we'll assume
is unknown untill runtime will "a" still get allocated ?)
What a particular compiler does is irrelevant; it is free to do
whatever it wants as long as the program semantics are correct.

Any help is greatly appreciated, just something i started
wondering about :) FMorales...

Fair enough.

HTH,
--ag


--
Artie Gold -- Austin, Texas
Oh, for the good old days of regular old SPAM.

Nov 13 '05 #5
There is no need to get nasty, I tried it on my system and then only
sent my post. Somebody already pointed out I was wrong.
No need for you to show your frustration here.
Bertrand Mollinier Toublet wrote:
Pushkar Pradhan wrote:
Did you try this?
In C you have to declare all variables first and then start your code.
So you would have to do
int a;
int i;

What you did is allowed in c++. I mean this code:
> int main(void)
> {
> int i = 0;
> if(!i) {
> int a;
> }
> return 0;
> }

Pushkar, you must have suffered a temporary access of madness. There is
nothing wrong with declaring variables at the beginning of a block. The
code above is perfectly valid C89 (as well as C99, of course).


Nov 13 '05 #6
On Sat, 04 Oct 2003 22:45:29 GMT, "FMorales" <al****@comcast .net>
wrote:
-- I didn't notice anything about this specifically
-- in the c.l.c. FAQ, nor could i get a strait answere
-- from my copy of the C standard. Hopeing someone
-- here could shed some light on it :)...

6.2.1
"If the declarator or type specifier that declares
the identifier appears inside a block or within the
list of parameter declarations in a function definition,
the identifier has block scope, which terminates at the
end of the associated block."

I believe this talks about what we learned early on :

int main(void)
{
{
int a;
}
printf("%d\n", a); /* ERROR - Invalid */
return 0;
}

Once outside of the closing } a is no longer accessible.
It's out of our current "scope". Which brings me to my
question. Since that data is static, is it automatically
allocated at program start up?

int main(void)
{
int i;
return 0;
}

As "i" would be in this example ? What if there's a conditional
statement there ? As in :

int main(void)
{
int i = 0;
if(!i) {
int a;
}
return 0;
}

If "a" is /NOT/ automatically allocated with "i", what is
the compiler doing to make sure it gets allocated only when,
and if it's ever needed (like if "i" changes before the
"if", or if it waits for input from a user which we'll assume
is unknown untill runtime will "a" still get allocated ?)

Any help is greatly appreciated, just something i started
wondering about :) FMorales...

You don't have any static objects in your sample code. While you
rethink the problem, consider that scope and duration (or life span)
have different meanings.
<<Remove the del for email>>
Nov 13 '05 #7
On Sat, 04 Oct 2003 21:23:08 -0500, Pushkar Pradhan wrote:

Bertrand Mollinier Toublet wrote:
Pushkar Pradhan wrote:
Did you try this?
In C you have to declare all variables first and then start your code.
So you would have to do
int a;
int i;

What you did is allowed in c++. I mean this code:
> int main(void)
> {
> int i = 0;
> if(!i) {
> int a;
> }
> return 0;
> }

Pushkar, you must have suffered a temporary access of madness. There is
nothing wrong with declaring variables at the beginning of a block. The
code above is perfectly valid C89 (as well as C99, of course).

There is no need to get nasty, I tried it on my system and then only
sent my post. Somebody already pointed out I was wrong.
No need for you to show your frustration here.


Are you claiming that the compiler you use on your system failed
to compile the code?
Nov 13 '05 #8
Actually I did it without the block {}, that's why it failed. I mean
if(a)
int i;

Sheldon Simms wrote:
On Sat, 04 Oct 2003 21:23:08 -0500, Pushkar Pradhan wrote:
Bertrand Mollinier Toublet wrote:
Pushkar Pradhan wrote:
Did you try this?
In C you have to declare all variables first and then start your code.
So you would have to do
int a;
int i;

What you did is allowed in c++. I mean this code:
> int main(void)
> {
> int i = 0;
> if(!i) {
> int a;
> }
> return 0;
> }
Pushkar, you must have suffered a temporary access of madness. There is
nothing wrong with declaring variables at the beginning of a block. The
code above is perfectly valid C89 (as well as C99, of course).


There is no need to get nasty, I tried it on my system and then only
sent my post. Somebody already pointed out I was wrong.
No need for you to show your frustration here.

Are you claiming that the compiler you use on your system failed
to compile the code?


Nov 13 '05 #9
I think it is allocated at runtime, I used gdb on:
int i = 0;
if(!i) {
int a;
}
return 0;
} When I tried to print a outside the loop gdb said:
No symbol "a" in current context
So it must have been created when the block is reached.

FMorales wrote: -- I didn't notice anything about this specifically
-- in the c.l.c. FAQ, nor could i get a strait answere
-- from my copy of the C standard. Hopeing someone
-- here could shed some light on it :)...

6.2.1
"If the declarator or type specifier that declares
the identifier appears inside a block or within the
list of parameter declarations in a function definition,
the identifier has block scope, which terminates at the
end of the associated block."

I believe this talks about what we learned early on :

int main(void)
{
{
int a;
}
printf("%d\n", a); /* ERROR - Invalid */
return 0;
}

Once outside of the closing } a is no longer accessible.
It's out of our current "scope". Which brings me to my
question. Since that data is static, is it automatically
allocated at program start up?

int main(void)
{
int i;
return 0;
}

As "i" would be in this example ? What if there's a conditional
statement there ? As in :

int main(void)
{
int i = 0;
if(!i) {
int a;
}
return 0;
}

If "a" is /NOT/ automatically allocated with "i", what is
the compiler doing to make sure it gets allocated only when,
and if it's ever needed (like if "i" changes before the
"if", or if it waits for input from a user which we'll assume
is unknown untill runtime will "a" still get allocated ?)

Any help is greatly appreciated, just something i started
wondering about :) FMorales...


Nov 13 '05 #10

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

Similar topics

11
8959
by: sks_cpp | last post by:
When do static members allocate memory? (at load time) How about static variables within a method? Where is the memory for static variables - is it allocated on the heap or does it have a region of its own?
6
3273
by: J Anderson | last post by:
Greetings, I was going through the quake2 source code (as you do) when I noticed arrays are statically allocated on the stack instead of being allocated dynamically. I’ve seen this before and know it’s for efficiency. I’m basically questioning the merit of such technique nowadays, for performance-orientated applications? Is it better to dynamically allocate a smaller array to fit the memory or to use one huge statically allocated...
24
2753
by: Steven T. Hatton | last post by:
In the following code, at what point is S::c fully defined? #include <iostream> using std::cout; using std::endl; using std::ostream; class C { int _v;
11
912
by: Jonan | last post by:
Hello, For several reasons I want to replace the built-in memory management with some custom built. The mem management itlsef is not subject to my question - it's ok to the point that I have nice and working allocation deallocation routines. However, I don't want to loose the nice extras of new operator, like - constructor calling, typecasting the result, keeping the array size, etc. For another bunch of reasons, outside this scope I...
11
3037
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
24
19065
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array is faster than malloc, but dynamic memory allocation is more flexible. Please comment... thanks.
7
8215
by: Jo | last post by:
Hi, How can i differentiate between static and dynamic allocated objects? For example: void SomeFunction1() { CObject *objectp = new CObject; CObject object;
53
26358
by: fdmfdmfdm | last post by:
This is an interview question and I gave out my answer here, could you please check for me? Q. What are the memory allocation for static variable in a function, an automatic variable and global variable? My answer: static variable in function and global variable are allocated in head, and automatic variable is allocated in stack. Right?
6
3552
by: Marvin Barley | last post by:
I have a class that throws exceptions in new initializer, and a static array of objects of this type. When something is wrong in initialization, CGI program crashes miserably. Debugging shows uncaught exception. How to catch an exception that happened before main() try { ... } catch (...) { ... } block? Is there a way?
10
4410
by: swornavidhya.mahadevan | last post by:
Which allocation (Static / Dynamic) is suitable for the situation when we are trying to allocate for a overloaded memory when the memory is full and no space to allocate. What will happen if both the allocation is impossible. Sworna vidhya
0
8249
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
8179
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
8685
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
8348
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
8493
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...
1
6112
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
4187
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2613
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
1493
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.