hello,
i am jeysree.i have a doubt.
please look at the following two snippets.it doesnt shows any error - #include<stdio.h>
-
#include<conio.h>
-
void main()
-
{
-
static int a[10]={1,2,3,4,5};
-
static int *p[10]={a,a+1,a+2,a+3,a+4};
-
.
-
.
-
.
-
.
-
getch();
-
}
- void main()
-
{
-
static int a[10]={1,2,3,4,5};
-
int *p[10]={a,a+1,a+2,a+3,a+4};
-
.
-
.
-
.
-
.
-
.
-
.
-
getch();
-
}
the following two shows errors - void main()
-
{
-
int a[10]={1,2,3,4,5};
-
int *p[10]={a,a+1,a+2,a+3,a+4};
-
.
-
.
-
.
-
.
-
.
-
getch();
-
}
- void main()
-
{
-
int a[10]={1,2,3,4,5};
-
static int *p[10]={a,a+1,a+2,a+3,a+4};
-
.
-
.
-
.
-
.
-
.
-
getch();
-
}
please explain the use of static here.the error shown is illegal initialisation
14 1917
Here is a simplified version of your snippets using CODE tags: - void sub1(void) {
-
static int a = 1;
-
static int *p = &a;
-
}
-
-
void sub2(void) {
-
static int a = 1;
-
int *p = &a;
-
}
-
-
void sub3(void) {
-
int a = 1;
-
static int *p = &a;
-
}
Variables defined within a block (such as within a function) can be either automatic or static. Automatic variables are instantiated at run-time each time the block is entered. Likewise, initial values for automatic variables are assigned at run-time each time the block is entered. Static variables are instantiated at compile-time. Initial values for static variables are magically assigned between the time you launch your program and when main is entered.
Consider a recursive function that contains definitions for one automatic and one static variable. There is no relationship or interaction between the instances of the automatic variable created each time the function calls itself. However, all instances of the function refer to the same single static variable. One instance will see changes made to the static variable by another instance.
The error you saw is a consequence of the difference between how initializers work for automatic and static variables. In all three examples, the definition of a is unremarkable. The error comes from the definition of pointer p, specifically the fact that its initializer is the address of a. - In sub1, p is a static variable. Thus its initial value must be known before the program starts to run. No problem, the address is known because a is also static.
- In sub2, p is an automatic variable. The initial value must be known each time the function is entered. No problem, the addresses of both static and automatic variables are known at run-time.
- In sub3, p is a static variable. The initial value must be known before the program starts to run. But now the initial value is the address of an automatic variable. Automatic variables don't exist until the function is called. There is no way to know what the address will be of something that doesn't exist yet.
@donbock
sir,
thanks for your reply.i just want to know when and where is memory allocated for static variables declared locally and globally?
also i wish to know when and where is memory allocated for other global variables other than static?
this would help me to understand better.
@donbock
sir,
thanks for your reply.i just want to know when and where is memory allocated for static variables declared locally and globally?
also i wish to know when and where is memory allocated for other global variables other than static?
this would help me to understand better.
I don't know what you mean by "static variables declared locally" or "static variables declared globally". A common source of confusion is how the keyword static is used for both linkage and storage duration.
Linkage applies to a variable's name. There are three kinds of linkage: external, internal, and none. External linkage means the name can be used in different source files to reference the same variable (that is, a global variable). Internal linkage means references to the variable name always refer to an instance of the variable local to the source file, The static keyword always establishes internal linkage. No linkage refers to function parameters and automatic variables.
Duration applies to a variable itself. There are three kinds of duration: static, automatic, and allocated. Variables with static duration exist for the entire duration of the program, from start to finish. Variables with automatic duration exist while execution remains within scope of the definition. Variables with allocated duration exist from the time they are explicitly allocated until they are explicitly freed. All variables declared with the static keyword plus all variables with external linkage have static duration. That is, all variables except function parameters and automatic variables have static duration.
My guess is that both "static variables declared locally" and "static variables declared globally" refer to variables with static duration. In that case, the variables are allocated and initialized automatically before the first line of your program is executed; and they persist until your program ends. Does this answer your question?
@donbock
sir,
some of the persons banfa,weaknessforcats in bytes.com replied that memory will be allocated for static variables inside function only at first time function is called.i am confused.the answers are in
allocation of memory
which i posted.please explain
I should rephrase my last paragraph:
My guess is that both "static variables declared locally" and "static variables declared globally" refer to variables with static duration. In that case, it is as if the variables are allocated and initialized automatically before the first line of your program is executed; and they persist until your program ends. Does this answer your question?
What error are you getting on your example using the static keyword?
As to your file types:
The .c is the text file withour code in it.
The .obj is the compiled version of the .c file.
If you program has 10 .c files then you will also have 10 .obj files.
The .exe file is created by the linker from the .obj files. The .exe is your executable program.
If you program has 10 .objh files then there will be a copy of each .obj placed in the .exe.
Then the linker then adds startup code to the .exe and resolves all missing addresses.
The .i files are the intermediate files created by the preprocessor from the .c files. Each .c is processed by the preprocessor and all of your #include result in a copy of the included file placed right where you coded the #include. This expanded .c file (the .i file) is called a translation unit and it is this file that is actually compiled.
@jeyshree
This same compiler error was the basis of one of your other threads. Didn't this reply explain why the error occurred?
Whether or not you believe in "compile-time" variable allocation, the fact remains that static variables are initialized once, before any instructions in that block are executed. Thus initial values for static variables have to be unambiguously defined before execution begins.
@weaknessforcats
Hello,
You say that memory for all variables will be allocated at run time only and compiler only generates code to allocate memory while compiling.Is it true for variables allocated using malloc and calloc(actually they say that it is dynamic memory allocation).Is run time alloction and dynamic memory allocation same?
@weaknessforcats
Hello,
I cant understand the word "linker" here.What is the use of linker (as you used in the following line)?
"The .exe file is created by the linker from the .obj files."
I merely cant understand the line
"If you program has 10 .objh files then there will be a copy of each .obj placed in the .exe."
Will there be many .obj files created for single .c program?
Why do use the word address in the forecoming statement?Does code to allocate memory take place here as you said (in the previous reply)?This doubt came to me because you told me in your previous reply that code to allocate memory will take place at compile time?
"Then the linker then adds startup code to the .exe and resolves all missing addresses."
The .i files are the intermediate files created by the preprocessor from the .c files. Each .c is processed by the preprocessor and all of your #include result in a copy of the included file placed right where you coded the #include. This expanded .c file (the .i file) is called a translation unit and it is this file that is actually compiled.
@donbock
Hello sir,
I get you sir .But many people still say memory to static variable inside a function will be allocated when it enters the function for the first time?If i take in that way
void sub3(void) {
int a = 1;
static int *p = &a;
}
should not throw any error.because static is allocated memory only after function is called and after memory for a is allocated.i now cant get into the final conclusion
@donbock
Hello,
You say memory for static variable inside a function will be allcated only at first time the function is called? Then why should the following code throw an error"illegal initialisation".
void main()
{
int a[10]={1,2,3,4,5};
static int *p[10]={a,a+1,a+2,a+3,a+4};
.
.
.
.
.
getch();
}
please explain.
@jeyshree
Sorry sir
I sent you the massage instead of sending for weaknessforcats .Please forgive me
I'm the one who first spoke of static variables being allocated at compile time. Let me explain what I meant. First of all, everything I am about to say is about C compilers -- things might work quite differently for C++. (Which are you using?) All of the C compilers I've worked with generate object files containing three memory segments: text, data, and bss. The text segment contains all of the executable code. The data segment contains all of the initialized variables that have static duration. The bss segment contains all of the uninitialized variables that have static duration. When I said "static variables are allocated at compile time" I was referring to the compiler defining a chunk of data or bss segment large enough to hold the variable. Weaknessforcats was right to point out that this is implementation-specific behavior that is not followed by all compiler implementations. Now that I've explained ... forget all about "compile-time allocation", it isn't really pertinent to why you get those compiler errors. - void sub3(void) {
-
int a = 1;
-
static int *p = &a;
-
}
The initializer for variable p is illegal because the C Standard says it is illegal. Much of my earlier discussion was aimed at explaining why the Standard would make this illegal -- but that's not important. What's important is that it is illegal because the Standard says it is illegal: the initializer for a static variable is not allowed to be the address of an automatic variable. If I had access to my copy of the C Standard I would quote it for you.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: BCC |
last post by:
Hi,
I have a class with several member variables that should be initialized
according to user input before an object is instantiated. Using a static
variable is required here.
But, I would...
|
by: katekukku |
last post by:
HI,
Could anyone please tell me what are static variables and what exactly
are there features.
I am a little bit confused.
Thank You
|
by: Mark Shelor |
last post by:
I've encountered a troublesome inconsistency in the C-language Perl
extension I've written for CPAN (Digest::SHA). The problem involves the
use of a static array within a performance-critical...
|
by: Wayne |
last post by:
Hi,
I'm new to .NET and have a question about the use of static variables vs.
session variables in a web form in C#.
Instead of using a session variable to hold a string to persist during...
|
by: Bryan Green |
last post by:
So I'm working on a project for a C# class I'm taking, where I need to
keep some running totals via static variables. I need three classes
for three different types of objects. The base class and...
|
by: Simone Chiaretta |
last post by:
I've a very strange behaveour related to a website we built:
from times to times, something should happen on the server, and all static variables inside the web application, both defined inside aspx...
|
by: Jesper Schmidt |
last post by:
When does CLR performs initialization of static variables in a class
library?
(1) when the class library is loaded
(2) when a static variable is first referenced
(3) when...
It seems that...
|
by: CDMAPoster |
last post by:
About a year ago there was a thread about the use of global variables
in A97:
http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157
Best Practices by Kang...
|
by: palani12kumar |
last post by:
can you please clear my doubt regarding static variables?
at the time of declaring a static variable, is it compulsary to declare its data type?
that is.... instead of writing "static int a;",...
|
by: RB |
last post by:
Hi clever people :-)
I've noticed a lot of people stating not to use static variables with
ASP.NET, and, as I understand it, the reason is because the variable is
shared across user sessions -...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |