LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-11-2005, 07:28 AM   #1
markhod
Member
 
Registered: Sep 2003
Posts: 103

Rep: Reputation: 15
checking pointer is non-null causes segmentation fault in c++


Hi,

In some c+ code I do;

if (A[0])

where A is some vector of pointers and simply doing this check causes a segmenation fault. I have no idea how to fix this (I checked the vector is within array bounds which is the only thing I could think of). Any ideas on some tests I could try?

Thankss,

Mark
 
Old 01-11-2005, 07:59 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
How about

Code:
if ( *A  )
 
Old 01-11-2005, 08:01 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
then again, silly me
what's the definition of A?

Is it static, are you initialising it?

need more info....
 
Old 01-11-2005, 08:30 AM   #4
kees-jan
Member
 
Registered: Sep 2004
Distribution: Debian, Ubuntu, BeatrIX, OpenWRT
Posts: 273

Rep: Reputation: 30
Basically, if a simple line like
Code:
if(A[0]))
is causing a segmentation fault, then A equals null. Try
Code:
if(A && A[0])
Groetjes,

Kees-Jan
 
Old 01-11-2005, 09:43 AM   #5
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
A doesn't have to necessarily be NULL to crash the program. It could be pointing to any invalid memory address.
Code:
{
  int *a;

  if(a[0])
    puts("foo");
}
That could also crash, but a is just random garbage, not necessarily NULL, and your check to see if A is NULL doesn't quite cover all the bases.
 
Old 01-11-2005, 09:46 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
exactly,

the definition at least may help.
 
Old 01-11-2005, 10:53 AM   #7
markhod
Member
 
Registered: Sep 2003
Posts: 103

Original Poster
Rep: Reputation: 15
Well the definition turns out to be not so simple.

I have a bunch of objects O and put them in a compnay-in-house container of O's (lets call it Cont).

In-House Cont;

for (int i = 0; i< some_value; i++){
Cont[i] = new O();
}

or (int i = 0; i< some_value; i++){
if (Cont[i]) //crash here
}

If I do Cont[i] I get returned a pointer to the ith O. It seems to me I know that new object exists so when I do Cont[i] I should get returned a valid pointer? Obviously I dont and since I see no reason for this I am stuck...

Cheers,

Mark
 
Old 01-11-2005, 10:57 AM   #8
markhod
Member
 
Registered: Sep 2003
Posts: 103

Original Poster
Rep: Reputation: 15
I am confused by some responses. A null pointer should not cause a crash just because I ask if it is null, surely? If it is null and I try to use the pointer to access a member function then I expect a crash, but not just from saying if(ptr). and theen only using it if it is non-null.
 
Old 01-11-2005, 11:08 AM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
yeh but
Cont[i] is referring to something called Cont
namely *(Cont +i)
Which if Cont is invalid, i.e. the container itself
then it will sigsev.
Or you are going over the end of the Cont container.

If in_house is just an array of pointers why use it?
why not use just an array of type O?
does the container have an initial size or is it smart enough (or not)
to resize?
 
Old 01-11-2005, 11:13 AM   #10
markhod
Member
 
Registered: Sep 2003
Posts: 103

Original Poster
Rep: Reputation: 15
>yeh but
>Cont[i] is referring to something called Cont
>namely *(Cont +i)
>Which if Cont is invalid, i.e. the container itself
>then it will sigsev.
>Or you are going over the end of the Cont container.

No, I checked the size of the container is 1 and the crash occurs when accessing the oth element...

>If in_house is just an array of pointers why use it?

because of rules governing how we persist data to the database (which has to have standard things for people to find when they analyse the data at a later date)

>why not use just an array of type O?
>does the container have an initial size or is it smart enough (or not) to resize?

it has no initial size. Its like a std::vector in that it resizes itself
 
Old 01-11-2005, 11:28 AM   #11
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by markhod
I am confused by some responses. A null pointer should not cause a crash just because I ask if it is null, surely?
True, but when you do "if (A[0]) ..." or "if (*A) ...", then you're already dereferencing the pointer A. In other words, such an if-statement is not checking if the pointer is null, but it's checking if the memory pointed to by A is null/0. If A itself is null, or if it points to an invalid address the program crashes because it reads from and memory address it may not have access to.

To test if pointer A is null, you could do:
Code:
if (A == NULL) ...
/* or: */
if (A) ....
Still, like itsme86 said, if it's not null, it may still contain an invalid address resulting in a segmentation fault, e.g. when a pointer is not initialized. This is probably what happens in your case:
Code:
for (int i = 0; i< some_value; i++){
    Cont[i] = new O();
}

for (int i = 0; i< some_value; i++){
    // if "some_value" is higher than "some_value" in
    // the loop above,...
    if (Cont[i]) // ...it will probably crash here when i
              //  exceeds the "some_value" of the first loop.
}

Last edited by Hko; 01-11-2005 at 11:33 AM.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika SUSE / openSUSE 2 12-02-2005 09:34 AM
NULL pointer dereference error Mercman2000 Linux - General 1 03-21-2005 09:36 PM
Segmentation fault on toplevel = XtVaAppInitialize(&app, "Email", NULL, 0, &argc, cbranje Programming 0 02-19-2005 12:06 PM
freeConfModules called with NULL pointer... opioid Linux - Software 1 10-31-2004 09:04 PM
Null Pointer vijeesh_ep Programming 3 08-13-2004 02:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration