0

I am trying to write a very simple pointer assignment in C++ like this:

float  *src;
if(someCondition == true)
    src = somePointer;
else
    src = someOtherPointer;

However, when I try to access src, I get a warning saying "Dereference of null pointer( loaded from variable src)"

Also, if I try to give some initial value to src like this:

float  *src = aPointer;

if(someCondition == true)
    src = somePointer;
else
    src = someOtherPointer;

I get a warning saying that the initial value assigned to src is never read. Even though this doesnt seem to be affecting what I am trying to do, I am trying to get rid of all potential issues. What seems to be the problem here?

Any help is appreciated. Thanks!

12
  • What is the type of someCondition? Commented Mar 5, 2013 at 19:56
  • What are somePointer and someOtherPointer? Commented Mar 5, 2013 at 19:57
  • Show us how somePointer and someOtherPointer are established please. Commented Mar 5, 2013 at 19:57
  • 2
    float * src = someCondition ? somePointer : someOtherPointer; Commented Mar 5, 2013 at 19:58
  • if(variableX > 350) src = &(anArray[0]) else src = &(anArray[50]). Thats the gist of the condition that I am trying to implement. Thanks! Commented Mar 5, 2013 at 19:58

3 Answers 3

3

Since your using pointer and not reference expect NULL to be a possible value of your pointers.

It might be in your interest to check for NULL:

if(!src) {/*handle null case*/}
Sign up to request clarification or add additional context in comments.

1 Comment

'always' is a bit strong. A program can establish and maintain non-null invariants and therefore avoid having to always check for null.
2

presumably one or both of somePointer and someOtherPointer are 0/NULL

2 Comments

No, i know for a fact that they are valid pointers. I am using them elsewhere too. Also, even though this is potentially unsafe because of the warning that I get, this piece of code is working just fine. It wouldn't work if any of the two pointers were NULL/0. I am just trying to get rid of the warning and the possibly unsafe situation.
it sounds to me like the compiler is seeing something you don't see. it might be some unexpected execution path, try to check them all. narrow in on the problem by commenting out blocks of code to see if/when the warning goes away.
0

It seems that the compiler isn't smart enough to realize that src will always be initialized. Although IDK why it would assume that the poiter would be NULL, more likely it would be some garbage value. This can probably be fixed by making the initialization a single statement using the ternary operator (?:) :

float* src = someCondition ? somePointer : someOtherPointer;

EDIT:

Try initializing src to NULL. That should get rid of the initialization warnings and I doubt that it will create an unused value warning.

2 Comments

I did try that, but it still gives the same warning. Inspite of the tertiary operator, the compiler doesnt realize that the pointer has been initialized. :(
Sorry, that didn't help either.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.