The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-17-2007
royalibrahim royalibrahim is offline
Registered User
  
 

Join Date: Jun 2007
Posts: 86
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;
}
}
  #2 (permalink)  
Old 08-17-2007
cbkihong cbkihong is offline Forum Advisor  
Advisor
  
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,624
C does not have try ... catch.
  #3 (permalink)  
Old 08-17-2007
andryk's Avatar
andryk andryk is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2003
Posts: 448
Quote:
Originally Posted by royalibrahim View Post
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;
}
}
One thing is for sure since you do not allocate memory for 'char *b', it is (surely) pointing to an invalid area thus the coredump ... To remove any doubt just try changing 'char *b' to 'char b[1]' and spot the difference
  #4 (permalink)  
Old 09-27-2007
royalibrahim royalibrahim is offline
Registered User
  
 

Join Date: Jun 2007
Posts: 86
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.
  #5 (permalink)  
Old 09-27-2007
porter porter is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 2,965
Code:
char *b;   /* =NULL */
....
cin >> *b;
means read a string to a null pointer?
  #6 (permalink)  
Old 10-05-2007
royalibrahim royalibrahim is offline
Registered User
  
 

Join Date: Jun 2007
Posts: 86
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..
  #7 (permalink)  
Old 10-20-2007
royalibrahim royalibrahim is offline
Registered User
  
 

Join Date: Jun 2007
Posts: 86
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..
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:20 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0