Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Reply
 
LinkBack Thread Tools Display Modes
Old 27-08-2011, 01:05 PM   #1 (permalink)
Right Off the Assembly Line
 
Join Date: Mar 2009
Location: hydrabad
Posts: 8
Default problem in out put while using pointer in C++


Code:
#include<iostream>
using namespace std;

class test
{
public:
    	int* i ;
       test(int k)
	   {
		i=&k;  
	   }

};

int main()
{
	
	test p(2);
    cout<<"for 1st time *(p.i) :"<< *(p.i);
	cout<<"\n";
	cout<<"for 2nd time *(p.i) :"<< *(p.i);
	cout<<"\n";
	cout<<"for 3rd time *(p.i) :"<< *(p.i);
	
   return 0;


}



Hi ,
The above c++ code that i compiled in visual c++,i got the following out put


output

for 1st time*(p.i) :2
for 2nd time *(p.i):20609120
for 3rd time *(p.i) :20609120


The first output is ok,but in 2nd and 3rd the out put is garbage .could any one please explain why this garbage values are coming in 2nd and 3rd?
priyaranjan is offline   Reply With Quote
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 27-08-2011, 02:46 PM   #2 (permalink)
BIOS Terminator
 
nims11's Avatar
 
Join Date: Apr 2008
Location: Ranchi
Posts: 816
Default Re: problem in out put while using pointer in C++

that's because you are passing a constant value to the constructor which is passed by value. when you pass 2 to the constructor, it is copied to a variable called k whose scope is local to the test() constructor. so when the constructor finishes its job, all left in the memory address of k (which i is pointing to) is some garbage value.

also, if you try to achieve the objective by using a pointer or references, passing a constant value like 2 will cause errors and you will have to store the value to some variable and then pass that variable (address).
nims11 is online now   Reply With Quote
Old 28-08-2011, 02:34 AM   #3 (permalink)
Right Off the Assembly Line
 
Join Date: Mar 2009
Location: hydrabad
Posts: 8
Default Re: problem in out put while using pointer in C++

Hello nims11,

Thank you very much for your reply.Still i have a doubt ,the thing is that when i have printed 1st time the constructor has finished its work ,so the out put at the first print statement should give the garbage out put ,but instead of that it is showing 2 which i passed as argument to the constructor.
priyaranjan is offline   Reply With Quote
Old 28-08-2011, 09:57 AM   #4 (permalink)
BIOS Terminator
 
nims11's Avatar
 
Join Date: Apr 2008
Location: Ranchi
Posts: 816
Default Re: problem in out put while using pointer in C++

it is compiler dependent. in mingw, all the output's were garbage.
nims11 is online now   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by Charan
- by Sarath
- by clmlbx

Advertisement




All times are GMT +5.5. The time now is 12:34 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2