Is it a problem due to scope or compiler ?

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2006
Posts: 80
Reputation: parthiban is an unknown quantity at this point 
Solved Threads: 6
parthiban's Avatar
parthiban parthiban is offline Offline
Junior Poster in Training

Is it a problem due to scope or compiler ?

 
0
  #1
May 15th, 2007
Hi all,

I tried one program to understand the concept of "Default Arguments" that program works prefectly in Dev C++ but not in Turbo C++ compiler(Ver 3.0).

Here's my program

  1. #include <iostream.h>
  2. #include <stdio.h>
  3. using namespace std;
  4.  
  5.  
  6.  
  7. void justcall(char *c="Parthiban");
  8. //char a[10]; //Global declaration needs for Turbo C++ Compiler
  9.  
  10. void main()
  11. {
  12. char *ch;
  13. char *accept();
  14. char flag='y';
  15. cout<<"Do u want to enter ur name:[y/n] ";
  16. cin>>flag;
  17. if(flag=='n')
  18. justcall();
  19. else
  20. justcall(ch=accept());
  21. fflush(stdin);
  22. cin.get();
  23. }
  24.  
  25. void justcall(char *a)
  26. {
  27. cout<<"Hello "<<a<<"!\n";
  28. fflush(stdin);
  29. cin.get();
  30. }
  31.  
  32. char* accept()
  33. {
  34. char a[10]; //Local declaration
  35. char *p;
  36. cout<<"Please enter ur name: ";
  37. cin>>a;
  38. p=a;
  39. return p;
  40. }
As i mentioned in the comment this program works only when character array ( a[10]) globally declared( for Turbo Compiler) .If i use locally defined array it displays garbage value .
I think it's scope vanished but how it is possible ?

Please tell me what is going on behind the scenes ?

and also clarify this doubt .

I need to call cin.get() twice ( main function justcall)function to temporary halt the program to view the results. Is it not enough to place only in main function(since anyhow the control will be return back to main function) .


Thanks in advance .
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 103
Reputation: mariocatch is an unknown quantity at this point 
Solved Threads: 17
mariocatch mariocatch is offline Offline
Junior Poster

Re: Is it a problem due to scope or compiler ?

 
0
  #2
May 15th, 2007
why is accept() not prototyped?
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Is it a problem due to scope or compiler ?

 
0
  #3
May 15th, 2007
I can't possibly cover everything here, but to make it short: that code is awful. Here's some links that I have lying around:

http://faq.cprogramming.com/cgi-bin/...&id=1043284351
http://faq.cprogramming.com/cgi-bin/...&id=1043284376
http://www.devx.com/tips/Tip/14447

>char *accept();
Yikes.

Since you're using C++, is there any particular reason why you're using C strings instead of the newer C++ ones?

>char a[10]; //Local declaration
You said it yourself: the variable is local. So what happens when the function returns? Well, of course, not only does the variable go out of scope, but the memory is deleted, and the pointer you return is to dead memory. You may want to use the static keyword to keep the memory for the life of the program.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 80
Reputation: parthiban is an unknown quantity at this point 
Solved Threads: 6
parthiban's Avatar
parthiban parthiban is offline Offline
Junior Poster in Training

Re: Is it a problem due to scope or compiler ?

 
0
  #4
May 15th, 2007
Originally Posted by mariocatch View Post
why is accept() not prototyped?
Hi mariocatch, I have prototyped inside main function.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 80
Reputation: parthiban is an unknown quantity at this point 
Solved Threads: 6
parthiban's Avatar
parthiban parthiban is offline Offline
Junior Poster in Training

Re: Is it a problem due to scope or compiler ?

 
0
  #5
May 15th, 2007
Thanks Joeprogrammer ,I'm just a beginner to C++ i will followthe standards in future .i have learnt a bunch of informations from your reply. Thanks a lot .

Even though the same program works prefectly in Dev C++ compiler without scope problem(i.e in Local scope) .

Please tell me how it is possible ?
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Is it a problem due to scope or compiler ?

 
0
  #6
May 15th, 2007
> Please tell me how it is possible ?
You're returning a pointer to a local variable.
So when the function returns, the local variable disappears, and the pointer is invalid.

Note that this does NOT GUARANTEE that your code will break. With undefined behaviour, absolutely anything can happen, including producing the expected answer (always a tricky one this, because it makes you think you're right, when in fact you're wrong).

> that program works prefectly in Dev C++ but not in Turbo C++ compiler(Ver 3.0).
Which is why it's best to learn the language to decide what is correct (or not).
Trying to learn C++ by observing the behaviour of a range of compilers is no way to go.

Essentially, you should be able to look at that code, see that a pointer to a local variable is being returned and decide that it is wrong without actually trying it.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 80
Reputation: parthiban is an unknown quantity at this point 
Solved Threads: 6
parthiban's Avatar
parthiban parthiban is offline Offline
Junior Poster in Training

Re: Is it a problem due to scope or compiler ?

 
0
  #7
May 15th, 2007
Thanks Salem, now i got the point .
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC