Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

about variable initialization in main method...

ultraphonicsultraphonics Member Posts: 34
That one was I believe replied to enough, but I need to know...

I do not quite believe that initialization of variables inside the main function (or any function for that matter) requires extra runtime overhead. If someone could point me to some literature that proves me wrong I would appreciate it. However, I believe that initialization of static variables is just that... static (i.e. at compile time) no matter where it is you choose to initialize them.

comments anyone?

Comments

  • PatLDDDPatLDDD Member Posts: 30
    : That one was I believe replied to enough, but I need to know...
    :
    : I do not quite believe that initialization of variables inside the main function (or any function for that matter) requires extra runtime overhead. If someone could point me to some literature that proves me wrong I would appreciate it. However, I believe that initialization of static variables is just that... static (i.e. at compile time) no matter where it is you choose to initialize them.
    :
    : comments anyone?
    :

    Hello,

    Well, I dont know well about initialisation of variable in function
    but the destruction code the compiller add at the end of the variable scope is certainly a overhead.So if you call your fuction often the construction and destruction is done every time you do so, this might result in some overhead but I think it is relatively small. But static variable are not compille-time...The variable must have a memory space and this is assigned at run time and then it put the value in it...These var last for the life of the excution but they must have destruction code to...The difference is that the construction and destruction is only done once during execution.

    Pat

  • ultraphonicsultraphonics Member Posts: 34
    : : That one was I believe replied to enough, but I need to know...
    : :
    : : I do not quite believe that initialization of variables inside the main function (or any function for that matter) requires extra runtime overhead. If someone could point me to some literature that proves me wrong I would appreciate it. However, I believe that initialization of static variables is just that... static (i.e. at compile time) no matter where it is you choose to initialize them.
    : :
    : : comments anyone?
    : :
    :
    : Hello,
    :
    : Well, I dont know well about initialisation of variable in function
    : but the destruction code the compiller add at the end of the variable scope is certainly a overhead.So if you call your fuction often the construction and destruction is done every time you do so, this might result in some overhead but I think it is relatively small. But static variable are not compille-time...The variable must have a memory space and this is assigned at run time and then it put the value in it...These var last for the life of the excution but they must have destruction code to...The difference is that the construction and destruction is only done once during execution.
    :
    : Pat
    :
    :

    yes, i realize that the assignment of memory is at runtime-but that is decided by the operating system. Every part of a program... including the code must be assigned memory space by the OS at runtime. The static variables however, already have space reserved for them in the program binary in the .data segment. I realize the overhead for local variables also, but you must remember that all local variables are declared on the stack, so the runtime performance hit is negligible, all that is required to allocate is one push operation, and to de-allocate, one pop operation. Also, the data area (for the stack) is already allocated.

    what I am getting at is the variables assigned in main themselves. The main method, since it is the program entry point is only called once per execution. So it seems to me that as an optimization, the main local variables could also be declared in the .data segment of the program binary. If this is the case, then there is no performance difference then making them static variables(globals). If on the other hand they are allocated otherwise, I concede that there may be an infinitesimal performance hit with the push operation, but the cleanup (the pop) is done at program termination, so who really cares?

    I may be wrong, but it all seems logical to me... what do you think?

    Ian

  • Shawn CarterShawn Carter Member Posts: 0

    ______ [ http://forcoder.org ] free ebooks and video tutorials about // Ruby, C++, C, Scratch, R, Python, Perl, JavaScript, Visual Basic .NET, Visual Basic, PHP, Swift, Java, Assembly, Delphi, C#, Go, MATLAB, PL/SQL, Objective-C Logo, Scheme, FoxPro, Erlang, Awk, Julia, Transact-SQL, Kotlin, Lua, ML, SAS, LabVIEW, Apex, Fortran, Crystal, F#, Rust, Ada, Scala, Clojure, Prolog, Hack, Lisp, Bash, Alice, VBScript, D, ABAP, COBOL, Dart // ____________

Sign In or Register to comment.