Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. storage for reference variable

storage for reference variable

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresperformancequestion
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    voorugonda prashanth
    wrote on last edited by
    #1

    memory for reference variable will be allocated or not? if yes,where does the reference variable get stored( stack or heap)? Thanks in advance. Prashanth.v -- modified at 3:07 Wednesday 3rd May, 2006

    C T K 3 Replies Last reply
    0
    • V voorugonda prashanth

      memory for reference variable will be allocated or not? if yes,where does the reference variable get stored( stack or heap)? Thanks in advance. Prashanth.v -- modified at 3:07 Wednesday 3rd May, 2006

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      What do you mean exactly ? Post some code to illustrate your question.

      V 1 Reply Last reply
      0
      • C Cedric Moonen

        What do you mean exactly ? Post some code to illustrate your question.

        V Offline
        V Offline
        voorugonda prashanth
        wrote on last edited by
        #3

        this question asked me in interview. i donot have any code. question is where does the reference variable get stored(stack or heap)? Prashanth.v

        1 Reply Last reply
        0
        • V voorugonda prashanth

          memory for reference variable will be allocated or not? if yes,where does the reference variable get stored( stack or heap)? Thanks in advance. Prashanth.v -- modified at 3:07 Wednesday 3rd May, 2006

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #4

          a reference is nothing more than a dereferenced pointer. when you create a reference variable, you create a variable first (before looking at its type), so the program NEED to store it somewhere in memory to keep its information.


          TOXCCT >>> GEII power

          [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

          J 1 Reply Last reply
          0
          • T toxcct

            a reference is nothing more than a dereferenced pointer. when you create a reference variable, you create a variable first (before looking at its type), so the program NEED to store it somewhere in memory to keep its information.


            TOXCCT >>> GEII power

            [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

            J Offline
            J Offline
            Justin Tay
            wrote on last edited by
            #5

            Isn't this an implementation detail? For example int a = 10; int &b = a; The compiler doesn't have to have memory for b, it can get the appropriate memory location from the symbol table. Of course when not possible to implement in this way compilers use a pointer to implement the reference.

            V 1 Reply Last reply
            0
            • J Justin Tay

              Isn't this an implementation detail? For example int a = 10; int &b = a; The compiler doesn't have to have memory for b, it can get the appropriate memory location from the symbol table. Of course when not possible to implement in this way compilers use a pointer to implement the reference.

              V Offline
              V Offline
              voorugonda prashanth
              wrote on last edited by
              #6

              solution please. whether it gets stored in stack or heap?

              J 1 Reply Last reply
              0
              • V voorugonda prashanth

                solution please. whether it gets stored in stack or heap?

                J Offline
                J Offline
                Justin Tay
                wrote on last edited by
                #7

                Unfortunately I think you should have clarified in the interview what was meant by reference variable. Say you had code like int a = 10; int &b = a; In this case it's not stored anywhere. The compiler just knows what you are referring to. If you had a function that looks like say DoSomething(int &b); int a = 10; DoSomething(a); Then the compiler has to implement the reference as passing a pointer to 'a' on the parameter list. Say this calling convention passes all arguments to functions on the stack, would this be considered storing in the stack?

                1 Reply Last reply
                0
                • V voorugonda prashanth

                  memory for reference variable will be allocated or not? if yes,where does the reference variable get stored( stack or heap)? Thanks in advance. Prashanth.v -- modified at 3:07 Wednesday 3rd May, 2006

                  K Offline
                  K Offline
                  kumar_mk
                  wrote on last edited by
                  #8

                  no memory is allocated to a reference variable. not in stack nor in heap.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups