General Questions
-
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.
-
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.
- 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
- 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
-
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.
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.
-
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.
-
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.
-
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();
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
-
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
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 -
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 -
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 -
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