checking pointer is non-null causes segmentation fault in c++
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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...
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.
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?
>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
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.
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.