![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Strange sed behaviour | vino | UNIX for Advanced & Expert Users | 8 | 02-12-2008 06:51 AM |
| A Strange Behaviour!!! | navojit dutta | Shell Programming and Scripting | 5 | 12-21-2007 04:35 AM |
| Count behaviour when using su - | Eronysis | Shell Programming and Scripting | 1 | 02-20-2006 06:20 PM |
| Can some 1 explain why this behaviour | helpmenow | High Level Programming | 2 | 12-18-2005 03:28 PM |
| Behaviour of default | soorajmu | High Level Programming | 5 | 12-09-2003 12:43 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Different behaviour of this program
Hi,
I have one doubt, in the below program, if I declare char *b inside the main(), the function compiles & runs properly. But at the same time, if I declare it globally it compiles but when we run it, it creates core dump (segmentation fault) both in C & C++. It is not being trapped by catch handler also. Do you know why? char *b; int main(){ //char *b; try { cout << "Enter char: "; //cin >> b; scanf("%c", b); cout << b << endl; } catch(...) { cout<<"An Exception has occured"<<endl; } } |
|
||||
|
The answers you gave doesn't answered my question. Ok, let's take it as it was compiled using C++ compiler, though it was not trapped using the catch() handler. And also my question hovers around declaring "b" in global and local scope. So please explain where the original problem lies.
|
|
||||
|
yes, that answered my question. Since the global variable stored in the data segment of BSS (Block Started by Symbol) memory area, it gets initialized to NULL (0 in precise), if not explicitly initialized while declaration. So writing to that memory causes Segmentation Fault core dump. And the local variable gets stored in the stack, which is initialized to some garbage value. so writing to that memory does not cause any core dump.
Thanks porter. Last edited by royalibrahim; 10-22-2007 at 05:37 AM.. |
|
||||
|
Hi, if I instantiate a local static object of type class A, inside a function foo() will the constructor of class A be called whenever I invoke this function or will it be invoked during program start-up automatically and only once? Also does the program behavior change in calling constructor if the function foo() is declared global function or not?
Last edited by royalibrahim; 10-22-2007 at 05:36 AM.. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|