We help IT Professionals succeed at work.

compile time and run time

nagaharikola
nagaharikola asked
on
As there are 4 storage classes,how the behavior of variable changed in compile time and run time
Comment
Watch Question

CERTIFIED EXPERT

Commented:

Author

Commented:
my question is how to differentiate their behavior in compile time and run time
What aspect of their behaviour are you interested in?

Author

Commented:
How to know the values assigned at compile time and run time
what is the difference between compile time and run time
CERTIFIED EXPERT

Commented:
evilrixSenior Software Engineer (Avast)
CERTIFIED EXPERT

Commented:
>> How to know the values assigned at compile time and run time
In general anything that is a constant expression will get calculated at compile time.

// trivial examples
short const y = 5;
int const i = 1 + 3 + y;
int const j = i / 2;

This includes any metaprogramming done with template constants.
http://en.wikipedia.org/wiki/Template_metaprogramming

C++03 didn't include functions in the category of a constant expression even if the function only worked with constants. The new C++11 fixes this.
http://en.wikipedia.org/wiki/C%2B%2B11#Generalized_constant_expressions
http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=315

>> what is the difference between compile time and run time
Well, it's pretty much what it says: compile time means the compiler does the calculations and runtime means the C runtime framework will do the calculations

Author

Commented:
farzanj,
the link you shared is good.
Do you have any material on the same topic in depth

Author

Commented:
Please can you tell me how memory allocation is done for variables ,arrays ,enums, structures and pointers , in compile time or run time.

Commented:
These appear to be standard assignment questions.
evilrixSenior Software Engineer (Avast)
CERTIFIED EXPERT

Commented:
And? The rules do not forbid asking for help, they forbid experts giving full solutions. That does not appear to have happened here.

Commented:
and the nagaharikola clearly isn't looking for full solutions to problems but rather someone to provide them with sufficient text to fill in the assignment.

My statement was a warning to the (clearly more aware than I gave credit for) responders.

The age of the last post from nagaharikola suggests the assignment due date may have passed.
evilrixSenior Software Engineer (Avast)
CERTIFIED EXPERT

Commented:
>> My statement was a warning to the (clearly more aware than I gave credit for) responders.
But, it's not your place to do that.

If you think there is a problem click "Request Attention" and inform the moderators... but before you do that it's probably worth taking account of the experts who have responded, checking their experience on the site and (as in my case) whether they are part of the site admin team.

Commented:
Thanks evilrix for your persuasive argument and encouraging tone.
I shall resume my "place".

Author

Commented:
I regret for the delay.
To clarify my confusion about the memory allocation of variables i asked this question.
This is a interview question where i was not able to satisfy the interviewer

I am interested in knowing the memory allocation during processing,compiling ,linking loading, running and what exactly happens in these phases.
Thats a very big question, each one of those is a involved topic.  It's actually a good interview question because there isn't one simple answer, you have to really do have to understand how programs work.  I think I might use that one.

I suggest you break it into smaller questions.  Not to get more points, because it would take such a long time to write the answer, and the experts don't all have so much time.  Each contains a lot of information.

I will request an admin highlights this question and puts it in other zones so that you get as many experts as possible involved.

Author

Commented:

Thanks for the response
Please do the needful to get rid of confusion

Commented:
Here is a link with an answer to the interview question which echoes the technical documentation.
http://kyapoocha.com/what-are-c-storage-classes.html

I strongly suggest you read
http://msdn.microsoft.com/en-us/library/y5f6w579.aspx
then read some more and write some code following the examples.

Make targetted changes to the code and try to predict the results before you run them.

Author

Commented:
I think the following question clarifies my question

In the stages of compilation(preprocessing,assembly ,compile and linking)
when does the stack is assigned?
when are the different segments(data and code) are assigned?
masheikSoftware Engineer
CERTIFIED EXPERT

Commented:
I will try to write the answer as simple as possible,

  Consider you are writing a simple program for that you have created two files, one .c say filex.c and one .h say filex.h , and your .c file contains all the necessary functions and local as well as static declations ,conditional compilation etc, you have written all the necessary prototypes of the functions,macros in filex.h file and included that file in filex.c.

  Now it is time of compilation ,Assume you are using unix machine and you are compiling the code throughn terminal,Assume you have n't write any make files to compile the code

  root/home/code $gcc -o testProgram filex.c

  It will create and executable  file testProgram in that file directory, and also it would create a file called filex.o object file in that directory.


  come to the point,

  At first , The initial stage of compilation is preprocessing which has the following things,
  macros,includes,conditional compilation,
  your .c file is feed into the preprocessor which do texual replacement ie your .c file contains include .h files(yes offcourse your standard include headers too) and contents of those files will be added into a seperate file  ie .i file and also which includes conditional compilation as well as macros

   you can see the .i file in unix/linux by using the followiing  option in gcc,

   root/home/code $gcc -i test.i filex.c
 
   The above command  generates  the test.i file,which contains your code + the replaced macros/includes/conditional compilation

  Ok we have only done the initial state, and we are not yet started any compilation process still,

  And now you have to know little bit about the process address space/how memory for your program is arranged

  There are 3 areas basically

                 stack ( local variables (block scope variables = { } )
                 data (Heap ,bss and data) (dynamic/uninitialized static/initialized static)
                 code/text area  (your binary code)

   Now sure you will get one question in mind , why do you need bss/data segments separately ?
   
    For optimization purpose,if not  included as in bss the final image size would be larger,
   

    Now come to compilation part
   
    When you feed the code file into compiler it creates assmebly file say for example the following command used to create assembler code

   $gcc -S filex.c

   The output file comes out with .s extension, which contains only assembly code

   The assembly code is then feed as input to the assembler which generates object file

    The object file which contains the relocation information /symbol table which only contains the information about the identifiers,scope,location

   For more information read the following sections,

   http://en.wikipedia.org/wiki/Symbol_table
   http://www.iis.sinica.edu.tw/~tshsu/compiler2003/slides/slide5.pdf
 

  and the output of the assembler is .obj(in windows) ,.o in unix/linux flavors

  which is then feed into the linker which actually creates the binary executable image

   The linker user the symbol table proveded by object file  and links all the functions libraries  to create a final executable image/binary static or shared library

  create a binary file from a c code and run the file in two different  time frames and  print a variables address in that code , both the time the addresses  are same or  different think about it.

  And one more thing,

  compile time is nothing but the time when you compiling the code(Think about compile time errors/warnings)
  Run time is offcourse ,the time when you are running the binary(run time error like segmentation fault)
 


   

Author

Commented:
Thanks masheik for the explanation

int x;
main()
{  
    int y;
    static int z;
}

In which stage does x,y and z gets the segments assigned (data,stack)

Please correct me if I am wrong
when we run the above program

Author

Commented:
please correct me if i am wrong
In the assembly stage symbol table is created and after linking stage the executable is generated which only contains the relocation information. this holds for all global and local variables.
In case of dynamic memory allocation. memory is assigned from heap.

masheikSoftware Engineer
CERTIFIED EXPERT

Commented:
int x;     / * Uninitialized global variable , goes to .bss segment */
main()
{  
    int y;             /* uninitialized local variable ,goes to stack segment */
    static int z;   /* Uninitialized static variable ,goes to .bss segment */
} 

Open in new window

uninitialized global/static  variables lifetime  is upto the end of  the program.
There is difference between scope and life (Read more about scope and life time of variables)
If you want more information then read this good tutorial,
http://www.tenouk.com/ModuleW.html

Author

Commented:
i understood the concept of segment allocation.
my question was
In what stages(compile, assembly ,linking,runtime) the data,stack , .bss are allocated
Software Engineer
CERTIFIED EXPERT
Commented:
Practically all the allocations, will be done in runtime only,ie when you are running the binary,

And It depends  on implementation also,

Theoretically, the static allocation (compile  time allocation will be done for variables whose sizes are known)

(stack - local variables with block scope)
(.bss   - static/global- static variables uninitialized)
(data  - text /initialized global variables , and again it depends)
see if you decalare  a static variable array of s,ize say 5MB ,your binary executable file size  will not increase..

dynamic allocation (runtime allocation for malloc,calloc,realloc pointer variables)
(heap- under data)

Explore More ContentExplore courses, solutions, and other research materials related to this topic.