C++ segmentation fault while checking for null pointer


 
Thread Tools Search this Thread
Top Forums Programming C++ segmentation fault while checking for null pointer
# 1  
Old 02-19-2010
C++ segmentation fault while checking for null pointer

Code:
void disptree(node *ptr)
{
    if ((ptr->left) !=NULL)
    disptree(ptr->left);

    cout<<"Position:"<<ptr->pos<<" Data:"<<ptr->data<<endl;

    if ((ptr->right)!=NULL;
    disptree(ptr->right);
}

i'm getting a segmentation fault at the red line. i cannot understand what's the problem. Help me out.
Thanks in advance..

Last edited by vino; 02-19-2010 at 02:47 AM.. Reason: added code tags
# 2  
Old 02-19-2010
You may also want to check if ptr is null or not. Something like

Code:
if (ptr && (ptr->left) !=NULL)

And likewise with ptr->right.
# 3  
Old 02-19-2010
Hi Vijay,

You are passing a pointer to this function. Ensure that the pointer is valid from where you called this fuction. It should not be pointing to deleted are freed objects.

-Vijay
# 4  
Old 02-21-2010
Thank you ppl

Thank u for ur time ppl. made a mistake at other part of prog so null was being to ptr...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

C. To segmentation fault or not to segmentation fault, that is the question.

Oddities with gcc, 2.95.3 for the AMIGA and 4.2.1 for MY current OSX 10.14.1... I am creating a basic calculator for the AMIGA ADE *NIX emulator in C as it does not have one. Below are two very condensed snippets of which I have added the results inside the each code section. IMPORTANT!... (11 Replies)
Discussion started by: wisecracker
11 Replies

2. Programming

Segmentation fault when I pass a char pointer to a function in C.

I am passing a char* to the function "reverse" and when I execute it with gdb I get: Program received signal SIGSEGV, Segmentation fault. 0x000000000040083b in reverse (s=0x400b2b "hello") at pointersExample.c:72 72 *q = *p; Attached is the source code. I do not understand why... (9 Replies)
Discussion started by: jose_spain
9 Replies

3. Programming

Using gdb, ignore beginning segmentation fault until reproduce environment segmentation fault

I use a binary name (ie polo) it gets some parameter , so for debugging normally i do this : i wrote script for watchdog my app (polo) and check every second if it's not running then start it , the problem is , if my app , remain in state of segmentation fault for a while (ie 15 ... (6 Replies)
Discussion started by: pooyair
6 Replies

4. Programming

segmentation fault.

This code is causing a segmentation fault and I can't figure out why. I'm new to UNIX and I need to learn how to avoid this segmentation fault thing. Thank you so much. Thanks also for the great answers to my last post.:):b: int main() { mysqlpp::Connection conn(false); if... (3 Replies)
Discussion started by: sepoto
3 Replies

5. Programming

Segmentation fault.

I'm getting a segmentation fault. I'm new to Linux programming. Thanks so much for all of your input.:eek: #include </usr/include/mysql++/mysql++.h> #include <stdio.h> #include <iostream> #include <sstream> #include <string.h> using namespace std; int outputToImport(const char*... (1 Reply)
Discussion started by: sepoto
1 Replies

6. Programming

segmentation fault

What is segmentation fault(core dumped) (1 Reply)
Discussion started by: gokult
1 Replies

7. Programming

segmentation fault

Hi, I am having this segmentation fault not in the following program, bt. in my lab program . My lab program is horrible long so cannot post it here bt. I am using the following logic in my program which is giving the segmentation fault. Bt. if I run this sample program as it is it dosen't give... (3 Replies)
Discussion started by: mind@work
3 Replies

8. Programming

Hi! segmentation fault

I have written a program which takes a directory as command line arguments and displays all the dir and files in it. I don't know why I have a problem with the /etc directory.It displays all the directories and files untill it reaches a sub directory called peers which is in /etc/ppp/peers.the... (4 Replies)
Discussion started by: vijlak
4 Replies

9. UNIX for Dummies Questions & Answers

Segmentation Fault

hello all, I tried a program on an array to intialise array elements from the standard input device.it is an integer array of 5 elements.but after entering the 4th element it throws a message called "Segmentation Fault" and returns to the command prompt without asking for the 5th element. ... (3 Replies)
Discussion started by: compbug
3 Replies

10. Programming

segmentation fault

hi all i'm trying to execute a c program under linux RH and it gives me segmentation fault, this program was running under unix at&t anybody kow what the problem could be? thanx in advance regards (2 Replies)
Discussion started by: omran
2 Replies
Login or Register to Ask a Question