| | |
Is it a problem due to scope or compiler ?
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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
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 .
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)
#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; }
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 .
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
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.
All my posts may be freely redistributed under the terms of the MIT license.
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 ?
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 ?
> 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.
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.
![]() |
Similar Threads
- XP Problem Due to Vista? (Windows Vista and Windows 7)
- Possible Font Book problem? (OS X)
- Simple Programming Errors (Computer Science)
Other Threads in the C++ Forum
- Previous Thread: help with dynamic memory
- Next Thread: check if the user enters a number instead of a letter
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph homeworkhelp iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






