Different behaviour of this program


 
Thread Tools Search this Thread
Top Forums Programming Different behaviour of this program
# 1  
Old 08-17-2007
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  
Old 08-17-2007
C does not have try ... catch. Smilie
# 3  
Old 08-17-2007
Quote:
Originally Posted by royalibrahim
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 Smilie
# 4  
Old 09-27-2007
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  
Old 09-27-2007
Code:
char *b;   /* =NULL */
....
cin >> *b;

means read a string to a null pointer?
# 6  
Old 10-05-2007
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 06:37 AM..
# 7  
Old 10-20-2007
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 06:36 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

different behaviour in fg and bg

fg = foreground bg = background I have a cobol program that I start with a very simple script. The script is not at fault as it has not changed and the program worked in fg and bg before. I have altered the logging in the program and moved my cursor declare to working storage. The program runs... (6 Replies)
Discussion started by: Bruble
6 Replies

2. Shell Programming and Scripting

cp -R behaviour

i 've noticed the following difference between freebsd cp and gnu cp from the freebsd cp man page: -R ... If the source_file ends in a /, the contents of the directory are copied rather than the directory itself. ... on gnu cp from the man pagewhile on gnu cp manpage: ‘-r'... (2 Replies)
Discussion started by: aegis
2 Replies

3. UNIX for Dummies Questions & Answers

Script to open program and send/execute command in program

Hi, i want to write a script that executes a program (exec?) . this program then requires a filename as input. how do i give it this input in the script so the program will be complete run and close by the script. e.g. exec prog.exe program then asks for filename "enter filename:"... (1 Reply)
Discussion started by: tuathan
1 Replies

4. UNIX for Dummies Questions & Answers

Strange Program behaviour

Had a strange thing going on with my code. It's ok I figured it out for myself.... (2 Replies)
Discussion started by: mrpugster
2 Replies

5. Shell Programming and Scripting

Why this behaviour of IF condition?

I have a variable, defndata, which is a number (fetched from a file using awk). I want that if defndata is not initialized (that is its not found in the file using awk), then to execute a block of statements, otherwise execute another block. if then .... else ... fi Now this... (4 Replies)
Discussion started by: indianjassi
4 Replies

6. Shell Programming and Scripting

A Strange Behaviour!!!

Can some-one give me a view to this : I have a directory in an unix server, having permissions r-xr-xr-x .This directory is basically a source directory. Now there is another directory basically the destination directory which has all the permissions. Note:I log in as not the owner,but user... (5 Replies)
Discussion started by: navojit dutta
5 Replies

7. Shell Programming and Scripting

Count behaviour when using su -

Gentlemen, OK, I have an odd issue here perhaps someone can shed some light for me. When running a script as its user/owner the below pause/wait works just fine. When a developer has used su - to assume the username the pause does not happen... This is a HPUX11.00 machine, I have copied the blurb... (1 Reply)
Discussion started by: Eronysis
1 Replies

8. Programming

Can some 1 explain why this behaviour

#include <iostream> using namespace std; int main() { const int l_test = 999999999; int *l_ptr = (int*) &l_test; cout<<"Constant Addr:"<<&l_test<<" Value:"<<l_test<<endl; cout<<"Pointer Addr:"<<l_ptr<<" Value:"<<*l_ptr<<endl; *l_ptr = 888888888; // Manipulating... (2 Replies)
Discussion started by: helpmenow
2 Replies

9. Programming

Behaviour of default

Hi, If I have the following code : int i=2; switch(i) { case 1 : {};break; default : { printf("d"); };break; case 2 : { printf("2"); };break; } what will be the output? I had an understanding that case statements are taken sequentially so default will be executed. But... (5 Replies)
Discussion started by: soorajmu
5 Replies
Login or Register to Ask a Question