Why if statement is not entering when Null - c++

I have the next code, and when raiz is NULL is not entering the if statement
I already tried to do the other way like if(raiz != NULL) and in the else statement trying to run the code :
masAlto = 1;
raiz = Nuevo;
Here is my code:
void InsertaAVL(arbolAVL *raiz, arbolAVL *Nuevo, int masAlto)
{
arbolAVL *a = raiz;
int subarbolmasAlto = FALSO;
if (raiz == NULL)
{
masAlto = 1;
raiz = Nuevo;
}
else
{
if (raiz->llave > Nuevo->llave)
{
//Insertar a la Izquierda
InsertaAVL(raiz->Izq, Nuevo, subarbolmasAlto);
if (subarbolmasAlto == VERDADerO)
{
switch(raiz->FBalance)
{
case LH : BalanceIzquierdo(raiz,masAlto);
break;
case RH : raiz->FBalance = EH;
masAlto = FALSO;
break;
case EH : raiz->FBalance = LH;
masAlto = VERDADerO;
break;
}
}
else
masAlto = FALSO;
}
else
{
InsertaAVL(raiz->Der, Nuevo, subarbolmasAlto);
if (subarbolmasAlto == VERDADerO)
{
switch(raiz->FBalance)
{
case LH : raiz->FBalance = EH;
break;
case RH : BalanceDerecho(raiz,masAlto);
break;
case EH : raiz->FBalance = RH;
masAlto = VERDADerO;
break;
}
}
else
masAlto = FALSO;
}
}
}
I expect that when raiz is NULL to enter the code inside the if statement

It does enter the if statement true branch, although I'd prefer if (!raiz) as the conditional as this behaves well with the NULL of C and the nullptr of C++. Effectively though the true branch is a no-op since the assignments
masAlto = 1;
raiz = Nuevo;
have no effect on the quantities passed by the caller of the function. Did you want int* masAlto &c. as the parameters?

Related

Why did one linked list's end node change from NULL to another list's next node?

The first function, linkAndMove, is used for basic linking together and moving point process.
The Union function is used for finding all numbers in linked lists la and lb (without repeats)
My test example: la {1,3} lb{3,5}
But in the last when la point to NULL, and lb point to 5.
After first function linkAndMove, the list la changed to {1,3,5}
Why did la's end node change from NULL to lb's now node 5?
before first function
after first function
void linkAndMove(slink **pNode, slink **qNode, slink **finNode,
int linkFlag, int moveFlag) {
if (linkFlag == -1 || moveFlag == -1) {
cout << "ERROR! No matched logical in basic link list process." << endl;
exit(1);
}
switch (linkFlag) {
case 0:
if ((*finNode)->data != (*pNode)->data) {
(*finNode)->next = (slink *) malloc(sizeof(MemLEN));
(*finNode)->next = (*pNode);
(*finNode) = (*finNode)->next;
}
break;
case 1:
if ((*finNode)->data != (*qNode)->data) {
(*finNode)->next = (slink *) malloc(sizeof(MemLEN));
(*finNode)->next = (*qNode);
(*finNode) = (*finNode)->next;
}
break;
case 2:
break;
default:
cout << "ERROR! No matched logical in basic link list process." << endl;
exit(1);
}
switch (moveFlag) {
case 0:
(*pNode) = (*pNode)->next;
break;
case 1:
(*qNode) = (*qNode)->next;
break;
case 2:
(*pNode) = (*pNode)->next;
(*qNode) = (*qNode)->next;
break;
default:
cout << "ERROR! No matched logical in basic link list process." << endl;
exit(1);
}
}
void Union(slink *la, slink *lb, slink *lc) {
slink *pNode, *qNode;
pNode = la->next;
qNode = lb->next;
int linkFlag, moveFlag;
while (pNode != NULL || qNode != NULL) {
linkFlag = -1;
moveFlag = -1;
if (pNode == NULL) {
linkFlag = moveFlag = 1;
} else if (qNode == NULL) {
linkFlag = moveFlag = 0;
} else {
if (pNode->data > qNode->data) {
linkFlag = 1;
moveFlag = 1;
} else if (pNode->data < qNode->data) {
linkFlag = 0;
moveFlag = 0;
} else {
linkFlag = 0;
moveFlag = 2;
}
}
/*if (pNode == NULL) {
linkAndMove(NULL, &qNode, &lc, linkFlag, moveFlag);
} else*/
linkAndMove(&pNode, &qNode, &lc, linkFlag, moveFlag);
}
}
I found the reason.
Because in function linkAndMove, the pointer finNode is connected to the list la's node. In preivous codes, using node's next to connect pNode, so changed the la's end node from NULL to that node.
The solution I found is create new node for list lc, that cannot infect the orignal data list la. Codes here.
switch (linkFlag) {
case 0:
if ((*finNode)->data != (*pNode)->data) {
(*finNode)->next = initLinkNode();
(*finNode) = (*finNode)->next;
(*finNode)->data = (*pNode)->data;
}
break;
case 1:
if ((*finNode)->data != (*qNode)->data) {
(*finNode)->next = initLinkNode();
(*finNode) = (*finNode)->next;
(*finNode)->data = (*qNode)->data;
}
break;
case 2:
break;
default:
cout << "ERROR! No matched logical in basic link list process." << endl;
exit(1);
}

Arduino : A case of switch block the case that are after

I've a switch and a functions 'block the case that are after it' at the 4th case of the switch.
I tried to move the case and apparently it's the 'P_JEU' case that block but I don't understand why.
switch (partie) {
case P_CHOIX_ANIM:
allPlayers.chenillard(250,100);
partie = P_CHOIX;
case P_CHOIX:
temp = allPlayers.checkInterro();
if (temp == 0) break;
if (temp == 1) { //Only 1 press
partie = P_JEU;
DEBUG_PRINTLN("P_CHOIX 1 press");
break;
}
partie = P_CHOIX_ERREUR;
break;
case P_JEU:
// Bug
if (bConfig.isPressed()) {
if (bConfig.getPressDuration()) {
if (bConfig.getPressDuration() <= 2000) {
partie = P_CHOIX_RESET;
} else {
bConfig.reset();
}
}
}
allPlayers.filteredCall(A_CHECK, J_PLAYER);
bool passResponse = allPlayers.Pass();
if (passResponse) { partie = P_JEU_REPONSE; }
break;
case P_CHOIX_RESET:
allPlayers.reset();
bConfig.reset();
partie = P_CHOIX_ANIM;
break;
default:
DEBUG_PRINTLN("Default");
partie = P_CHOIX_RESET;
break;
Thanks
You cant declare variables inside a case:
https://complete-concrete-concise.com/programming/c/keyword-switch-case-default/
Can I declare variables inside an Objective-C switch statement?
bool passResponse = allPlayers.Pass();
is wrong - you have to enclose it:
case P_JEU:
// Bug
if (bConfig.isPressed()) {
if (bConfig.getPressDuration()) {
if (bConfig.getPressDuration() <= 2000) {
partie = P_CHOIX_RESET;
} else {
bConfig.reset();
}
}
}
allPlayers.filteredCall(A_CHECK, J_PLAYER);
{
bool passResponse = allPlayers.Pass();
if (passResponse) { partie = P_JEU_REPONSE; }
}
break;

mesh.delete_face() caused abort()

I have tried to run the code below but got a error when using mesh.delete_face(*it, false);
vector<TriMesh::FaceHandle> terminalFaces;
OpenMesh::FPropHandleT<int> faceType;
OpenMesh::FPropHandleT<bool> faceChecked;
mesh.add_property(faceType, "face-type");
mesh.add_property(faceChecked, "face-checked");
tFillFacesProperty(mesh, faceChecked, false);
tClassifyFaces(mesh, faceType);
terminalFaces = tSplitJointFaces(mesh, faceType);
tClassifyFaces(mesh, faceType);
tAdjustAllVertices(mesh);
for (int i = 0; i < terminalFaces.size(); i++)
{
if (mesh.property(faceType, terminalFaces[i]) != FT_TERMINAL)
continue;
if (mesh.property(faceChecked, terminalFaces[i]))
continue;
vector<TriMesh::VertexHandle> collectedVs;
vector<TriMesh::FaceHandle> collectedFs;
for (TriMesh::FaceVertexIter fvi = mesh.fv_begin(terminalFaces[i]);
fvi != mesh.fv_end(terminalFaces[i]);
++fvi)
{
if (mesh.valence(*fvi) == 2) {
collectedVs.push_back(*fvi);
break;
}
}
assert(collectedVs.size() == 1);
while(1){
bool mustStop = false;
if (collectedFs.empty())
collectedFs.push_back(terminalFaces[i]);
else {
TriMesh::FaceHandle nextFh;
for (TriMesh::FaceFaceIter ffi = mesh.ff_begin(collectedFs.back());
ffi != mesh.ff_end(collectedFs.back());
++ffi)
{
if ((find(collectedFs.begin(), collectedFs.end(), *ffi) == collectedFs.end())
&& mesh.is_valid_handle(*ffi)
&& mesh.property(faceType, *ffi) != FT_NEW)
{
nextFh = *ffi;
break;
}
}
if (mesh.is_valid_handle(nextFh))
collectedFs.push_back(nextFh);
else
mustStop = true;
}
TriMesh::FaceHandle curfh = collectedFs.back();
mesh.property(faceChecked, curfh) = true;
int t = mesh.property(faceType, curfh);
if (t == FT_SLEEVE || t == FT_TERMINAL) {
TriMesh::HalfedgeHandle hh2check;
for (TriMesh::FaceHalfedgeIter fhi = mesh.fh_begin(curfh);
fhi != mesh.fh_end(curfh);
++fhi)
{
TriMesh::FaceHandle oppof = mesh.opposite_face_handle(*fhi);
if (mesh.is_valid_handle(oppof)
&& (find(collectedFs.begin(), collectedFs.end(),oppof) == collectedFs.end()))
{
hh2check = *fhi;
break;
}
}
assert(mesh.is_valid_handle(hh2check) && !mesh.is_boundary(hh2check));
// add new vertices
TriMesh::VertexHandle right, left;
right = mesh.from_vertex_handle(hh2check);
left = mesh.to_vertex_handle(hh2check);
assert(mesh.is_valid_handle(left) && mesh.is_valid_handle(right));
if ((find(collectedVs.begin(), collectedVs.end(), left) == collectedVs.end())
&& mesh.is_valid_handle(left))
collectedVs.insert(collectedVs.begin(), left);
if ((find(collectedVs.begin(), collectedVs.end(), right) == collectedVs.end())
&& mesh.is_valid_handle(right))
collectedVs.push_back(right);
bool inCircle = true;
TriMesh::Point center = (mesh.point(right) + mesh.point(left)) / 2.0;
double diamSq = tSqDist(mesh.point(right), mesh.point(left));
for (int i = 1; i < collectedVs.size() - 1; i++)
{
TriMesh::VertexHandle vh = collectedVs[i];
if (tSqDist(mesh.point(vh), center) * 4 > diamSq) {
inCircle = false;
break;
}
}
if (inCircle && !mustStop) {
continue;
}
else {
OpenMesh::IO::write_mesh(mesh, "outputtr.off");
for (vector<TriMesh::FaceHandle>::iterator it = collectedFs.begin(); it != collectedFs.end(); ++it){
mesh.delete_face(*it, false);
}
I used delete_face() in openmesh to remove one of the faces in mesh.
However, debugging always shows abort() and the error panel just like:
R6010 -abort() has been called
and the breakpoint was triggered by _CrtDbgBreak():
case 1: _CrtDbgBreak(); msgshown = 1;
Maybe the error is caused by a dangling reference/pointer, or an invalidated iterator. How could I fix it?
Just add "mesh.request_face_status();" and solved.

How to fix this statement may fall through [-Werror=implicit-fallthrough=]?

What does
this statement may fall through [-Werror=implicit-fallthrough=]
mean ?
I getting this error while compiling at statement like this:
switch(eT)
{
case SEL_CRIT:
{
TYPE1* psSel;
iRetVal = dbseq(enB->m_ps,
NULL, NULL, &esM, NULL, ESEC);
while (iRetVal == 0)
{
if(psEnterprise)
{
bool iFound = false;
for (i = 0; i< psME->m_pslave[0].m_uc; i++)
{
ENT node1;
sEOS = psME>m_pslave[0].m_pslavecnt[i];
}
if (iFound && (psME->m_NOTOVERLOADED == false))
{
return psME;
}
}
}
psSel = (M_EN*)pCrit;
LOG_INFO(FAIL_TO_LOAD, psME->m_ONG, psME->EN);
int_Enterprise = NULL;
}
at
int_Enterprise = NULL;
where
int_Enterprise is some structure pointer.
How can I fix this?
You have no break; at the end of your case: so execution will fall through into the next case. Add a break statement to prevent fall-through if that's what you want or add a [[fallthrough]] attribute if fallthrough is intended.

add unary operator in expression tree

I am doing my assignment in which I have to modify my previous task (which is prefix expression tree which takes expression and give result)
+ OR
* AND
- NOT
Now I have to make it Logic expression tree which will perform AND OR and NOT operations
char input;
cin.get(input);
if((input == '+')||(input == '-')||(input == '*'))
{
p = new ExprTreeNode(input,NULL,NULL);
buildSub(p->left);
buildSub(p->right);
}
else if(isdigit(input))
{ //create a new node
p = new ExprTreeNode(input,NULL,NULL);
}
else
{
cout <<" invalid expression exiting..." <<endl;
exit (1);
}
above code reads expression and makes tree using recursion...
I am confused how I can add unary operator that is NOT...
after that I have to evaluate expression
int answer;
switch (p->dataItem){
case '*':
// AND
case'+':
// OR
case '-':
// Reverse
default:
answer = (p->dataItem-'0');
break;
}
return answer;
p is ExprTreeNode
// Data members
char dataItem; // Expression tree data item
ExprTreeNode *left, // Pointer to the left child
*right; // Pointer to the right child
I'd say you want to do the following:
if ((input == '+') || (input == '*'))
{
p = new ExprTreeNode(input,NULL,NULL);
buildSub(p->left);
buildSub(p->right);
}
else if (input == '-')
{
p = new ExprTreeNode(input, NULL, NULL);
buildSub(p->left);
}
else if(isdigit(input))
{ //create a new node
p = new ExprTreeNode(input,NULL,NULL);
}
else
{
cout <<" invalid expression exiting..." <<endl;
exit (1);
}
Edit:
Then, the evaluation routine could work like this:
bool ExprTreeNode::evaluate() {
switch (dataItem) {
case '+':
return left->evaluate() || right->evaluate();
case '*':
return left->evaluate() && right->evaluate();
case '-':
return !left->evaluate();
case '0':
return false;
default:
return true;
}
}