User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 426,136 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,747 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1010 | Replies: 6 | Solved
Reply
Join Date: Sep 2006
Location: India
Posts: 79
Reputation: parthiban is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 6
parthiban's Avatar
parthiban parthiban is offline Offline
Junior Poster in Training

Help Is it a problem due to scope or compiler ?

  #1  
May 14th, 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

#include <iostream.h>
#include <stdio.h>
using namespace std;



void justcall(char *c="Parthiban");
//char a[10]; //Global declaration needs for Turbo C++ Compiler 

void main()
{
 char *ch;
 char *accept();
 char flag='y';
 cout<<"Do u want to enter ur name:[y/n] ";
 cin>>flag;
 if(flag=='n')
     justcall();
 else
    justcall(ch=accept());
 fflush(stdin);
 cin.get();
}

void justcall(char *a)
 {
  cout<<"Hello "<<a<<"!\n";
  fflush(stdin);
  cin.get();
 }

 char* accept()
 {
  char a[10]; //Local declaration
  char *p;
   cout<<"Please enter ur name: ";
   cin>>a;
   p=a;
   return p;
 }

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 .
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2007
Posts: 102
Reputation: mariocatch is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 17
mariocatch mariocatch is offline Offline
Junior Poster

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

  #2  
May 15th, 2007
why is accept() not prototyped?
Reply With Quote  
Join Date: Apr 2006
Location: Canada
Posts: 4,503
Reputation: John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light 
Rep Power: 17
Solved Threads: 275
Moderator
Featured Blogger
John A's Avatar
John A John A is offline Offline
Vampirical Moderator

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

  #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.
tuxation.com - Linux articles, tutorials, and discussions
Reply With Quote  
Join Date: Sep 2006
Location: India
Posts: 79
Reputation: parthiban is an unknown quantity at this point 
Rep Power: 3
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 ?

  #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  
Join Date: Sep 2006
Location: India
Posts: 79
Reputation: parthiban is an unknown quantity at this point 
Rep Power: 3
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 ?

  #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  
Join Date: Dec 2005
Posts: 3,638
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 22
Solved Threads: 417
Colleague
Salem's Avatar
Salem Salem is offline Offline
void main'ers are DOOMed

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

  #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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Reply With Quote  
Join Date: Sep 2006
Location: India
Posts: 79
Reputation: parthiban is an unknown quantity at this point 
Rep Power: 3
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 ?

  #7  
May 15th, 2007
Thanks Salem, now i got the point .
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 4:41 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC