Make c++ interpret a pointer to NULL as zero - c++

I have this kind of code
two->height = max(two->right->height, two->left->height);
One of the two->right or two->left can be a pointer to null so the program will seg fault . I am looking for , if the two->left is null it will get transformed into zero so the two->right will be automatically true .
Is there any trick that can overcome this issue ?

This can also work:
two->height = max(
( two->right != nullptr ? two->right->height : 0 ),
( two->left != nullptr ? two->left->height : 0 )
);

You are first going to want to perform a check on the left and right pointers and see if they are null. Something along the lines of:
if(two->right == NULL) {
...
}
else if(two->left == NULL) {
...
}
else {
two->height = max(two->right->height, two->left->height);
}
There are many ways to deal with pointers being NULL. I just picked a simple one for an example.

Related

sqlite and possible null value

Sqlite3 table have INT column with possible NULL values.
I add the data to the table with this code:
if ( ptr == NULL )
sqlite3_bind_null(stmt, 5);
else
sqlite3_bind_int64(stmt, 5, ptr->key_session );
then I get back my values with the following code:
unsigned int key_session = (unsigned int)sqlite3_column_int64(stmt, 0);
So, what is about NULL? What is the best practice with NULLs in the table? how to check for NULL? or what will occur in my GET code with NULL ?
You can do this check:
if (sqlite3_column_type(stmt, 0) == SQLITE_NULL)
...
If the check succeeds, the value was NULL; otherwise, it was a non-null int64.
You can check whether sqlite_column_type() returns SQLITE_NULL.
If 0 is a valid return for nulls, you don't need to do anything special - nulls will convert to zero if an integer is requested.

Correct use of Syntax in if statement using && Operator and 2 variables

I have a quick question regarding proper use of syntax, basically i am trying to summarise these two if statements into one if statement.
if (sc.LastCallToFunction) {
if (p_LowRectanglesList != NULL) {
free(p_LowRectanglesList);
sc.PersistVars->i1 = 0;
}
if (p_HighRectanglesList != NULL) {
free(p_HighRectanglesList);
sc.PersistVars->i2 = 0;
}
return;
Would it be syntactically correct to rewrite this as:
if (sc.LastCallToFunction) {
if (p_LowRectanglesList || p_HighrectangleList != NULL) {
free(p_LowRectanglesList && p_HighRectanglesList);
sc.PersistVars->i1 && sc.PersistVars->i2 = 0;
}
return;
Or would the compiler not accept this / Is my Logic faulty?
you can't do it the way you have given
if (p_LowRectanglesList || p_HighrectangleList != NULL)
this logically ORs the first pointer ( treats it as true or false ) with the comparison of the seond pointer to NULL
free(p_LowRectanglesList && p_HighRectanglesList);
this logically &&s the pointers together and then tries to free the result of that operation. ie, you are trying to free "true" or "false"
sc.PersistVars->i1 && sc.PersistVars->i2 = 0;
this logically ands the two things together, which will result in true or false and then trys to assign 0 to it..... doesn't make any sense at all.
Also, in your original code....after the free, you should put p_LowRectanglesList=NULL;

iterate char** why does this work?

I picked up a this piece of code I copy past to my program. This seems to be a new way to me to iterate through char**:
char** vArray; // The array containing values
// Go throught properties
if(szKey == "KeyMgmt")
{
vArray = (char**)g_value_get_boxed((GValue*)value);
for( ; vArray && *vArray ; vArray++) // Why does this work ?!
pWpaKey->addKeyMgmt(std::string(*vArray));
}
else if(szKey == "Pairwise")
{
// ...
}
It looks like to work like a charm but I don't understant why! vArray is Supposed to contain an adress right? And *vArray the "string" value. So why when I "AND" an address with its value this give me an equality?
vArray && *vArray is equivalent to (vArray != NULL) && (*vArray != NULL)
It's first checking that the pointer vArray isn't NULL and, assuming it is not NULL, checking that the pointer it points to isn't NULL.
The loop condition is
vArray && *vArray
This is basically shorthand for
(vArray != 0) && (*vArray != 0)
which is true if the char** pointer is non-null and points to a char* which is non-null.

linux c/c++ - weird if/else issue

I'm querying a mysql table which then loops through the results.
One of the fields has a value of "0" in it, so when I try the following it doesn't work!
while ((row2 = mysql_fetch_row(resultset2)) != NULL) {
if (row2[2] != "0") {
// the field has a value of 0, but it's still executing the code here!
} else {
// should be executing this code
}
}
I know C/C++ is very strict when it comes variables (unlink php), but I can't figure this one out. Anyone have any ideas why?
You're comparing row2[2], a pointer to char, with a pointer to the constant char array "0".
Use strcmp(row2[2], "0") != 0 (C solution), std::string(row2[2]) != "0" (C++ solution), or atoi(row2[2]) != 0 if you know row2[2] is always the string representation of an integer (and cannot be a SQL NULL value).
You cannot compare string literal like this :
if (row2[2] != "0") //wrong
Either you do this :
if (strcmp(row2[2], "0")) //correct
Or this:
if (std::string(row2[2]) != "0") //correct
For this particular case, when there is only one character you can also do this:
if (row2[2][0] != '0') //correct - not the single quote around 0!

NULL Pointer Problem?

void Insert(AVLnode * & root, string X)
{
if ( root == NULL)
{
root = GetNode(X);
}
else if ( root->info > X )
{
Insert(root->left,X);
if ( height(root->left) - height(root->right) == 2 )
if ( X < root->left->info )
RotateRR(root);
else
RotateLR(root);
}
else if ( root->info < X )
{
Insert(root->right,X);
if ( height(root->right) - height(root->left) == 2 )
if ( X > root->right->info )
RotateRR(root);
else
RotateLR(root);
}
root->height = max( height(root->left), height(root->right) )+1;
}
AVLnode* find(AVLnode* root,string X)
{
if (!root) return 0;
if ( root->info == X )
return root;
else if (root->info < X)
find(root->left,X);
else if (root->info > X)
find(root->right,X);
return 0;
}
int main(int argc,char* argv)
{
AVLnode* Dic;
Insert(Dic,"adf");
return 0;
}
The first time in Insert, root is NULL but when I debug, it skips root == null. What's going on?
The problem is in the AVLnode* Dic; statement of main(). You are sending an uninitialized pointer to insert() from main(). It contains garbage values. Initialize it to NULL
The first time it's not NULL. Variables do not auto-initialize to NULL or 0 in C++; they contain garbage (whatever the memory address they occupy contained before).
Replace AVLnode* Dic; with AVLnode* Dic = NULL; and try again.
"The first time in Insert", in the code you give, root is a "random"-ish value, because you never initialize Dic -- so it may well happen to be NULL when you're running without a debugger and otherwise if you are using a debugger. That's just a definite bug in your code, so add = 0 before the ; in the first line of main's body. After which, you may find more bugs (hard to say, since you don't show us GetNode or the Rotate thingies, even though you do show us the find function that's never ever called -- peculiar choice of code to show us, indeed).