compile time and run time

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeyshree
    New Member
    • Jul 2010
    • 75

    compile time and run time

    Hello,
    Will someone please explain me what is compile time and run time?They say memory for static variable will be allocated at compile time?Does it mean it is allocated during creation of .exe file?Please explain if i were wrong.I am a beginner so please explain me clearly.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    The code to perform the allocation is generated by the compiler. The actual allocation cannot occur until the program is running.

    Comment

    • jeyshree
      New Member
      • Jul 2010
      • 75

      #3
      Originally posted by weaknessforcats
      The code to perform the allocation is generated by the compiler. The actual allocation cannot occur until the program is running.
      Hello,
      You say that only code will be generated for allocation of memory.Is this true only for static variables?Becau se auto(or local) or local variables of a function should be allocated memory only when the function is called i.e during program execution.Will that be run time allocation?Or for that too code will be generatedduring compile time?Please clearly explain me what is compile time and run time?

      Thanks for your reply

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Lety me ask you a question:

        If you compile on machine A and run the program on machine B, then how can memory be allocated at compile time on machine A?

        Obviously, all memory is allocated at run time.

        The compiler only generates the code to do the allocating but does not do the actual allocating.

        It's not that hard:
        1) variables outsode functions are allocated at run time before main() starts.
        2) variables inside functions are allocated when the function is called. In C++ there will be variables allocated after the function is called but before it completes.

        A static variable inside a function is not kept with the other variables since it does not belong to a specific function call. Instead, this static variable is kept in an area of memory that will not be deallocated when the function completes.

        The compiler generates code to create an initialize the static variable on the first call and on all opther calls, generates code that uses the same variable created on the first call.

        Remember, you are not running the program when you compile it.

        Comment

        • jeyshree
          New Member
          • Jul 2010
          • 75

          #5
          Originally posted by weaknessforcats
          Lety me ask you a question:

          If you compile on machine A and run the program on machine B, then how can memory be allocated at compile time on machine A?

          Obviously, all memory is allocated at run time.

          The compiler only generates the code to do the allocating but does not do the actual allocating.

          It's not that hard:
          1) variables outsode functions are allocated at run time before main() starts.
          2) variables inside functions are allocated when the function is called. In C++ there will be variables allocated after the function is called but before it completes.

          A static variable inside a function is not kept with the other variables since it does not belong to a specific function call. Instead, this static variable is kept in an area of memory that will not be deallocated when the function completes.

          The compiler generates code to create an initialize the static variable on the first call and on all opther calls, generates code that uses the same variable created on the first call.

          Remember, you are not running the program when you compile it.
          Hello,
          Thanks for your reply.I understood what is compile time and run time.Will you please answer me an another question for which I didnot ge answer till now?
          Will you please explain me the different process involved in conversion of .c file to .exe file .They say first .i files will be created.Followe d by .obj files and followed by .exe files.will you please explain all these things?What is the difference between .obj file and .exe file?(I am using turbo c in windows 7 with intel core 2 duo E7500 2.93 ghz processor).Than ks a lot.
          I am a beginner.So please explain in a way I can understand.Main ly i cant understand in which process of compilation code to allocate memory will be done?

          Comment

          • jeyshree
            New Member
            • Jul 2010
            • 75

            #6
            Originally posted by weaknessforcats
            Lety me ask you a question:

            If you compile on machine A and run the program on machine B, then how can memory be allocated at compile time on machine A?

            Obviously, all memory is allocated at run time.

            The compiler only generates the code to do the allocating but does not do the actual allocating.

            It's not that hard:
            1) variables outsode functions are allocated at run time before main() starts.
            2) variables inside functions are allocated when the function is called. In C++ there will be variables allocated after the function is called but before it completes.

            A static variable inside a function is not kept with the other variables since it does not belong to a specific function call. Instead, this static variable is kept in an area of memory that will not be deallocated when the function completes.

            The compiler generates code to create an initialize the static variable on the first call and on all opther calls, generates code that uses the same variable created on the first call.

            Remember, you are not running the program when you compile it.
            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.
            edit reply report

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              What error are you getting?

              This compiles and links OK with Visual Studio.NET 2008.

              Comment

              • jeyshree
                New Member
                • Jul 2010
                • 75

                #8
                Originally posted by weaknessforcats
                What error are you getting?

                This compiles and links OK with Visual Studio.NET 2008.
                The error shown is "illegal initialisation" in turbo compiler.Why are you speakin about visual studio.net?

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  Visual Studio.NET is the C/C++ compiler that I use.

                  This compiler follows the C/C++ 1998 standard pretty closely.

                  Comment

                  • Banfa
                    Recognized Expert Expert
                    • Feb 2006
                    • 9067

                    #10
                    Code:
                    int main()
                    {
                        int a[10]={1,2,3,4,5};
                        static int *p[10]={a,a+1,a+2,a+3,a+4};
                    
                        return 0;
                    }
                    Using gcc on Linux this compiles as C++ but does not compile as C.

                    Comment

                    • jeyshree
                      New Member
                      • Jul 2010
                      • 75

                      #11
                      Originally posted by jeyshree
                      The error shown is "illegal initialisation" in turbo compiler.Why are you speakin about visual studio.net?
                      I am speaking about turbo c compiler.
                      Please forgive me for disturbing you.You said code to allocate memory for all variables will be generated at compile time.will that be true for static variable inside a function?
                      Code:
                      #include<stdio.h>
                              #include<conio.h>
                              #include<alloc.h>
                              void fun()
                              {
                               int a;
                               static int b;
                              }
                              void main()
                              {
                                int c;
                                float d;
                                int *p;
                                int n;
                                clrscr();
                                printf("enter n value");
                                scanf("%d",&n);
                                fun();/*fun is called in main.so will code  be generated to allocate memory for a(each time the function is called) and b(only first time function is called)when this line is compiled*/
                                p=malloc(10);/*will code to allocate memory for this variable take place in compile time?or it is different from previous variables*/
                                p=malloc(n*2);/*will code to allocate memory for this variable take place in runtime?*/                                                     
                             }
                      Please explain me clearly .Remember me .I am a beginner.

                      Comment

                      • jeyshree
                        New Member
                        • Jul 2010
                        • 75

                        #12
                        Originally posted by Banfa
                        Code:
                        int main()
                        {
                            int a[10]={1,2,3,4,5};
                            static int *p[10]={a,a+1,a+2,a+3,a+4};
                        
                            return 0;
                        }
                        Using gcc on Linux this compiles as C++ but does not compile as C.
                        Hello,
                        I cannot understand your last post which explains about compile time and runrime allocation due to a small spelling mistake made.Now I understood that code to allocate memory for static variable and initialising it take place as soon as call to the function is made at the first time.Please explain me the next two lines in my question to you.
                        p=malloc(10);
                        p=malloc(n*2);
                        Thanks

                        Comment

                        • jeyshree
                          New Member
                          • Jul 2010
                          • 75

                          #13
                          Originally posted by weaknessforcats
                          Visual Studio.NET is the C/C++ compiler that I use.

                          This compiler follows the C/C++ 1998 standard pretty closely.
                          I am speaking about turbo c compiler.
                          Please forgive me for disturbing you.You said code to allocate memory for all variables will be generated at compile time.will that be true for static variable inside a function?
                          #include<stdio. h>
                          #include<conio. h>
                          #include<alloc. h>
                          void fun()
                          {
                          int a;
                          static int b;
                          }
                          void main()
                          {
                          int c;
                          float d;
                          int *p;
                          int n;
                          clrscr();
                          printf("enter n value");
                          scanf("%d",&n);
                          fun();/*fun is called in main.so will code be generated to allocate memory for a(each time the function is called) and b(only first time function is called)when this line is compiled*/
                          p=malloc(10);/*will code to allocate memory for this variable take place in compile time?or it is different from previous variables*/
                          p=malloc(n*2);/*will code to allocate memory for this variable take place in runtime?*/
                          }
                          Please explain me clearly .Remember me .I am a beginner.

                          Comment

                          • jeyshree
                            New Member
                            • Jul 2010
                            • 75

                            #14
                            Originally posted by jeyshree
                            I am speaking about turbo c compiler.
                            Please forgive me for disturbing you.You said code to allocate memory for all variables will be generated at compile time.will that be true for static variable inside a function?
                            #include<stdio. h>
                            #include<conio. h>
                            #include<alloc. h>
                            void fun()
                            {
                            int a;
                            static int b;
                            }
                            void main()
                            {
                            int c;
                            float d;
                            int *p;
                            int n;
                            clrscr();
                            printf("enter n value");
                            scanf("%d",&n);
                            fun();/*fun is called in main.so will code be generated to allocate memory for a(each time the function is called) and b(only first time function is called)when this line is compiled*/
                            p=malloc(10);/*will code to allocate memory for this variable take place in compile time?or it is different from previous variables*/
                            p=malloc(n*2);/*will code to allocate memory for this variable take place in runtime?*/
                            }
                            Please explain me clearly .Remember me .I am a beginner.
                            Hello,
                            I cannot understand your last post which explains about compile time and runrime allocation due to a small spelling mistake made.Now I understood that code to allocate memory for static variable and initialising it take place as soon as call to the function is made at the first time.Please explain me the next two lines in my question to you.
                            p=malloc(10);
                            p=malloc(n*2);
                            Thanks

                            Comment

                            • jeyshree
                              New Member
                              • Jul 2010
                              • 75

                              #15
                              Originally posted by jeyshree
                              I am speaking about turbo c compiler.
                              Please forgive me for disturbing you.You said code to allocate memory for all variables will be generated at compile time.will that be true for static variable inside a function?
                              #include<stdio. h>
                              #include<conio. h>
                              #include<alloc. h>
                              void fun()
                              {
                              int a;
                              static int b;
                              }
                              void main()
                              {
                              int c;
                              float d;
                              int *p;
                              int n;
                              clrscr();
                              printf("enter n value");
                              scanf("%d",&n);
                              fun();/*fun is called in main.so will code be generated to allocate memory for a(each time the function is called) and b(only first time function is called)when this line is compiled*/
                              p=malloc(10);/*will code to allocate memory for this variable take place in compile time?or it is different from previous variables*/
                              p=malloc(n*2);/*will code to allocate memory for this variable take place in runtime?*/
                              }
                              Please explain me clearly .Remember me .I am a beginner.
                              Hello,
                              Again sorry .But i dont have any way.If you reply thos post for sure i will understand better.
                              I cannot understand your last post which explains about compile time and runrime allocation due to a small spelling mistake made.Now I understood that code to allocate memory for static variable and initialising it take place as soon as call to the function is made at the first time.Please explain me the next two lines in my question to you.
                              p=malloc(10);
                              p=malloc(n*2);
                              Thanks

                              Comment

                              Working...