943,935 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1954
  • C++ RSS
May 15th, 2007
0

Is it a problem due to scope or compiler ?

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  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 .
Similar Threads
Reputation Points: 10
Solved Threads: 6
Junior Poster in Training
parthiban is offline Offline
80 posts
since Sep 2006
May 15th, 2007
0

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

why is accept() not prototyped?
Reputation Points: 11
Solved Threads: 17
Junior Poster
mariocatch is offline Offline
103 posts
since Apr 2007
May 15th, 2007
0

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

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.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
May 15th, 2007
0

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

Click to Expand / Collapse  Quote originally posted by mariocatch ...
why is accept() not prototyped?
Hi mariocatch, I have prototyped inside main function.
Reputation Points: 10
Solved Threads: 6
Junior Poster in Training
parthiban is offline Offline
80 posts
since Sep 2006
May 15th, 2007
0

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

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 ?
Reputation Points: 10
Solved Threads: 6
Junior Poster in Training
parthiban is offline Offline
80 posts
since Sep 2006
May 15th, 2007
0

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

> 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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
May 15th, 2007
0

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

Thanks Salem, now i got the point .
Reputation Points: 10
Solved Threads: 6
Junior Poster in Training
parthiban is offline Offline
80 posts
since Sep 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: help with dynamic memory
Next Thread in C++ Forum Timeline: Creating Console Applications Using Visual C++ Express Edition 2005





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC