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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. General Questions

General Questions

Scheduled Pinned Locked Moved C / C++ / MFC
performancequestionlounge
10 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.
  • R Offline
    R Offline
    rajaratk
    wrote on last edited by
    #1

    Hello, I need the answers for the following questions: 1. I have a class (ex: Test) contains 2 variables,then I am creating 2 objects of that class. How many copies of the variables are stored in memory? 2. What will be the output of the following program: int* fun() { int a; return &a; } suppose, if i assign a value for the variable "a" (ie int a=10) what will be the output? Thanks in advance for the solutions.

    R M R 4 Replies Last reply
    0
    • R rajaratk

      Hello, I need the answers for the following questions: 1. I have a class (ex: Test) contains 2 variables,then I am creating 2 objects of that class. How many copies of the variables are stored in memory? 2. What will be the output of the following program: int* fun() { int a; return &a; } suppose, if i assign a value for the variable "a" (ie int a=10) what will be the output? Thanks in advance for the solutions.

      R Offline
      R Offline
      Russell
      wrote on last edited by
      #2
      1. two: one copy for each object. Do you want one common object for every class? Then you have to use a static variable. 2) it returns the address of the variable 'a'...it is not important what it contains (10 , 20 or 0). You can look at the contents of (&a) with *****( &a )

      Russell

      1 Reply Last reply
      0
      • R rajaratk

        Hello, I need the answers for the following questions: 1. I have a class (ex: Test) contains 2 variables,then I am creating 2 objects of that class. How many copies of the variables are stored in memory? 2. What will be the output of the following program: int* fun() { int a; return &a; } suppose, if i assign a value for the variable "a" (ie int a=10) what will be the output? Thanks in advance for the solutions.

        M Offline
        M Offline
        Matthew Faithfull
        wrote on last edited by
        #3

        Suspiciously homeworky :~ I reckon you can multiply 1 by 2 so on to the second part. The a varaible is a local varibale in the function you have written. This along with the way it's declared mean that it ends up on the stack. If you think what happens to the stack for this function when execution leaves the function then you'll understand what returning &a will give you. If the stack means nothing to you then you've discovered the point of the exercise. If you're into full on low level code and just not familiar with C++ then I'd recommend debugging through the example you posted in the disassembly window.

        Nothing is exactly what it seems but everything with seems can be unpicked.

        1 Reply Last reply
        0
        • R rajaratk

          Hello, I need the answers for the following questions: 1. I have a class (ex: Test) contains 2 variables,then I am creating 2 objects of that class. How many copies of the variables are stored in memory? 2. What will be the output of the following program: int* fun() { int a; return &a; } suppose, if i assign a value for the variable "a" (ie int a=10) what will be the output? Thanks in advance for the solutions.

          R Offline
          R Offline
          Russell
          wrote on last edited by
          #4

          rajaratk wrote:

          int* fun() { int a; return &a; }

          bad function: you can't use the address of a local variable, 'a' is destroyed when the function ends, so *fun() can return a number different from 10 (in your example)


          Russell

          J 1 Reply Last reply
          0
          • R rajaratk

            Hello, I need the answers for the following questions: 1. I have a class (ex: Test) contains 2 variables,then I am creating 2 objects of that class. How many copies of the variables are stored in memory? 2. What will be the output of the following program: int* fun() { int a; return &a; } suppose, if i assign a value for the variable "a" (ie int a=10) what will be the output? Thanks in advance for the solutions.

            R Offline
            R Offline
            rajaratk
            wrote on last edited by
            #5

            Thanks a lot for your answers. For Q1, I have one member function like fun(), if i am creating 2 objects and calling the function, how many copies are stored in the memory: Test t1, t2; t1.fun(); t2.fun();

            M 1 Reply Last reply
            0
            • R rajaratk

              Thanks a lot for your answers. For Q1, I have one member function like fun(), if i am creating 2 objects and calling the function, how many copies are stored in the memory: Test t1, t2; t1.fun(); t2.fun();

              M Offline
              M Offline
              Maximilien
              wrote on last edited by
              #6

              Why don't you suggest an answer, and from there we will be glad to help you; because now it seems we are doing all the hard work.


              Maximilien Lincourt Your Head A Splode - Strong Bad

              1 Reply Last reply
              0
              • R Russell

                rajaratk wrote:

                int* fun() { int a; return &a; }

                bad function: you can't use the address of a local variable, 'a' is destroyed when the function ends, so *fun() can return a number different from 10 (in your example)


                Russell

                J Offline
                J Offline
                jhwurmbach
                wrote on last edited by
                #7

                Russell` wrote:

                you can't use the address of a local variable,

                You can, but the value gained us useless, thats the pitty!


                Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
                George Orwell, "Keep the Aspidistra Flying", Opening words

                R 1 Reply Last reply
                0
                • J jhwurmbach

                  Russell` wrote:

                  you can't use the address of a local variable,

                  You can, but the value gained us useless, thats the pitty!


                  Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
                  George Orwell, "Keep the Aspidistra Flying", Opening words

                  R Offline
                  R Offline
                  Russell
                  wrote on last edited by
                  #8

                  So, finally, it is the same of "you can't use it".:laugh: Do you agree?


                  Russell

                  J 1 Reply Last reply
                  0
                  • R Russell

                    So, finally, it is the same of "you can't use it".:laugh: Do you agree?


                    Russell

                    J Offline
                    J Offline
                    jhwurmbach
                    wrote on last edited by
                    #9

                    Yeah, your right. My brain-internal search&replace put 'get' in place of the 'use' you had written!


                    Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
                    George Orwell, "Keep the Aspidistra Flying", Opening words

                    R 1 Reply Last reply
                    0
                    • J jhwurmbach

                      Yeah, your right. My brain-internal search&replace put 'get' in place of the 'use' you had written!


                      Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
                      George Orwell, "Keep the Aspidistra Flying", Opening words

                      R Offline
                      R Offline
                      Russell
                      wrote on last edited by
                      #10

                      jhwurmbach wrote:

                      My brain-internal search&replace put 'get' in place of the 'use' you had written!

                      Damned multithreading...:laugh::laugh:


                      Russell

                      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