c++ - Resolving compiler error: pointer potentially uninitialized -


i need resolve error compiler picking on - understand why it's picking on error need resolve since function (throwing error) execute when pointer is initialized.

here pseudo code:

if (incoming_message_exists)  {     msg_class* current_msg;      /*current_msg become either value_1 or value_2*/      /*code block 1*/     if (condition_is_fulfilled)     {         current_msg = value_1;     }      /*code block 2*/     else      {         current_msg = value_2;     }      /*code block 3*/     /*bool function performed on current_msg throwing error*/     if (function(current_msg))     {         //carry out function      } } 

i prefer not perform code block 3 within 1 , 2, if that's solution will. in advance!

are if , else branches show 2 different if statements?

if are, current code capable of leaving current_msg uninitialised. crash when reach function(current_msg).

if you've shown 2 branches same if statement, compiler wrong - there no danger of current_msg not being initialised. may still need change code suppress warning however, if build warnings errors.

you can fix/suppress warning initialising current_msg when declared

msg_class* current_msg = null; 

if don't have other code in either branch, initialise using ternary operator

msg_class* current_msg = condition_is_fulfilled? value_1 : value_2; 

if warning genuine, you'll have either check function copes being passed null argument or guard against this

if (current_msg != null && function(current_msg)) 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -