3 Replies - 267 Views - Last Post: 09 September 2011 - 04:58 PM Rate Topic: -----

#1 nautica17  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 155
  • Joined: 28-August 09

When exactly does a variable get created

Posted 09 September 2011 - 04:09 PM

I have a curious question regarding C++. Say I have some function (other than main()) that has some variable declared inside it, let's just say it's an int. Is that variable created the moment I invoke the function with the said variable, or is it created before I even invoke the function?

Edit: I am thinking that it's created at compile time. Not sure why, but I always just assumed that all variables were created in the beginning. So I'm curious if my assumption is correct.

This post has been edited by nautica17: 09 September 2011 - 04:10 PM


Is This A Good Question/Topic? 0
  • +

Replies To: When exactly does a variable get created

#2 timetraveller  Icon User is offline

  • New D.I.C Head

Reputation: -25
  • View blog
  • Posts: 10
  • Joined: 09-September 11

Re: When exactly does a variable get created

Posted 09 September 2011 - 04:24 PM

all parameters that are passed to functions are created during runtime... for example int somefunction(int a,int b );
then those a and b are made during runtime. And r pushing into a location in the RAM called the stack.

But incase of the usual local and global variables defined inside the functions it is created during "Program Loading". This program loading is done by the operating system. It allocates memory in the CPU main memory(the RAM) and even in some registers(if u put register int a or something like that) and thats how memory gets allocated

This post has been edited by timetraveller: 09 September 2011 - 04:25 PM

Was This Post Helpful? 3
  • +
  • -

#3 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4462
  • View blog
  • Posts: 7,776
  • Joined: 08-June 10

Re: When exactly does a variable get created

Posted 09 September 2011 - 04:39 PM

View Postnautica17, on 09 September 2011 - 06:09 PM, said:

Edit: I am thinking that it's created at compile time. Not sure why, but I always just assumed that all variables were created in the beginning. So I'm curious if my assumption is correct.


Variables exist in the scope of the program's execution. Compiling a program does not run it, so no variables are created. Think about it. If I compiled a program on my computer (and it created variables), how would I be able to use the program on other computers? You don't recompile programs when you install them (for the most part).

You might be misunderstanding the term "compile time", if all you've done is compile and run from the IDE. "Compile time" refers to the actual compilation of the program. When your compiler builds the executable from your code. "Run time" is when you execute the executable, so to speak. Lots of IDEs do these steps in sequence for you when you hit the "run" or "debug" buttons, but they're distinct steps.
Was This Post Helpful? 2
  • +
  • -

#4 nautica17  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 155
  • Joined: 28-August 09

Re: When exactly does a variable get created

Posted 09 September 2011 - 04:58 PM

View PostCurtis Rutland, on 09 September 2011 - 05:39 PM, said:

View Postnautica17, on 09 September 2011 - 06:09 PM, said:

Edit: I am thinking that it's created at compile time. Not sure why, but I always just assumed that all variables were created in the beginning. So I'm curious if my assumption is correct.


Variables exist in the scope of the program's execution. Compiling a program does not run it, so no variables are created. Think about it. If I compiled a program on my computer (and it created variables), how would I be able to use the program on other computers? You don't recompile programs when you install them (for the most part).

You might be misunderstanding the term "compile time", if all you've done is compile and run from the IDE. "Compile time" refers to the actual compilation of the program. When your compiler builds the executable from your code. "Run time" is when you execute the executable, so to speak. Lots of IDEs do these steps in sequence for you when you hit the "run" or "debug" buttons, but they're distinct steps.


Yea run time is what I meant to say instead. Not sure why I said compile time. But is the variable created in memory before or after the function is invoked is what I'm wondering. I think timetraveller might have answered my question above, but any other details are welcome. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1