Thread: variables

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    1

    variables

    Hi,

    When you declare a variable such as int length, does the identifier part (length) hold the actual address and when you assign say: length =5 then the identifier is deferenced? or is this incorrect what im assuming?

    thank you

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I am not a compiler expert, so someone will probably come along and correct me, but: The compiler can do it any way it wants to, as far as I understand the C standard.

    When you do length = 5, the 5 has to be put somewhere in memory, that is true. I would expect that memory location to be found by the compiler via a look-up table, which may or may not fit your notion of "dereferenced".

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I'd say tabstop is right. If the compiler can prove that in fact length is effectively a constant, it may even be able to simply remove the length variable and replace it with the constant value.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    The compiler builds a symbol table which holds variable names. It looks up any uses of variables and builds the memory addresses right into the resultant code. In the finished code, unless symbolic information is kept for a run-time debugger, the variable names are not saved anywhere. If an executable consists of multiple C modules that share variables then symbol information is saved in the .obj's until the linker makes use of them to tie things together. In the end they are discarded also.

    joe345 makes correct assumptions as to how one can think of how things work, conceptually. In keeping with C nomenclature of address, dereferencing, etc. How things work in practice in the interest of code speed, computer hardware utilization, things may be implemented differently.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Accessing Variables
    By vileoxidation in forum C++ Programming
    Replies: 10
    Last Post: 10-05-2009, 07:58 AM
  2. Protected / Private Variables accessable.
    By +Azazel+ in forum C++ Programming
    Replies: 19
    Last Post: 09-08-2009, 07:39 PM
  3. Question about Static variables and functions.
    By RealityFusion in forum C++ Programming
    Replies: 2
    Last Post: 10-14-2005, 02:31 PM
  4. Remotely Creating Variables
    By Rajin in forum C++ Programming
    Replies: 1
    Last Post: 04-26-2005, 11:20 PM
  5. Declaring an variable number of variables
    By Decrypt in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2005, 04:46 PM